streamwriter

Raised exception in the case of concurrent file access with StreamReader

烂漫一生 提交于 2019-12-07 14:04:56
问题 I found a post talking about handling concurrent file access with StreamWriter. The problem is that the answers do not manage the scenario where the file is being accessed but multiple processes. Let's tell it shortly : I have multiple applications I need a centralised logging system in dataBase If database fail, I need a fallback on a file system log There is a known concurrency scenario, where multiple applications (processes) will try to write in that file. This can be managed by re

Save file - xmlSerializer

我与影子孤独终老i 提交于 2019-12-07 00:18:08
问题 I'm creating a method to serialize a file using this code: public void Save(Object file, Type type, String path) { // Create a new Serializer XmlSerializer serializer = new XmlSerializer(typeof(type)); // Create a new StreamWriter StreamWriter writer = new StreamWriter(@path); // Serialize the file serializer.Serialize(writer, file); // Close the writer writer.Close(); } But Visual Studio tells me this when I attempt to build: "Error 1 The type or namespace name 'type' could not be found (are

Remove all previous text before writing

江枫思渺然 提交于 2019-12-06 18:55:30
问题 I am writing a text file and each time i write i want to clear the text file. try { string fileName = "Profile//" + comboboxSelectProfile.SelectedItem.ToString() + ".txt"; using (StreamWriter sw = new StreamWriter(("Default//DefaultProfile.txt").ToString(), true)) { sw.WriteLine(fileName); MessageBox.Show("Default is set!"); } DefaultFileName = "Default//DefaultProfile.txt"; } catch { } How do I do this? I want to remove all previous content from DefaultProfile.txt . I actually have to know

Multi-threaded TcpClient send timeout after long open connection

元气小坏坏 提交于 2019-12-06 12:43:18
问题 I'm having a problem with a TcpClient closing with a send timeout in a multi-threaded application after the connection has been open for a long period of time (several hours or overnight). The NetworkStream is being used by two threads, a UI thread and a background thread. There is a StreamReader used for reading incoming data, and a StreamWriter used for outgoing data. The StreamReader is only ever accessed one thread (the background one), but the StreamWriter is accessed by both the UI

Adding to text file without deleting

穿精又带淫゛_ 提交于 2019-12-06 04:12:26
I need to add 4 variable numbers into a single line but using the current code it just replaces the numbers thus making it pointless. using (StreamWriter writer = new StreamWriter("build.txt")) { writer.Write(" " + layer + " " + x + " " + y + " " + block); Console.WriteLine("[" + layer + "," + x + "," + y + "," + block + "]"); } so I needed it to write... l x y b l x y b l x y b l x y b ... ...but it did not work out like that, and the internet wasn't particularly helpful, hopefully here can help. I would use File.AppendAllText , personally... That way you can just specify the text you want to

How to transform an xml structure generated from a request to a web services

♀尐吖头ヾ 提交于 2019-12-06 02:54:34
I have a string var that store an xml from a request to a RESTful service. I have a problem transforming this with an xslt file on a fly without saving it. I am getting this error System.UriFormatException: Invalid URI: The Uri scheme is too long. On this line xslt.Transform(xmldoc, null, writer); string xmldoc = xReq("http://restful.com/RestAPI"); XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(@"C:\Users\XSeXml\xRes.xslt"); string htmlOutput; StringWriter writer = new StringWriter(); xslt.Transform(xmldoc, null, writer); htmlOutput = writer.ToString(); Literal1.Text =

How to Write to a file using StreamWriter?

て烟熏妆下的殇ゞ 提交于 2019-12-06 00:57:05
问题 in my Wpf app I'm using a class Person (that is a base class), and that contains a virtual method SaveData(), and other class Client that inherits from Person. How to override method SaveData() and keeping data from base? Class Person public virtual void SaveData() { string arqName = string.Format("Person{0}" + ".txt", Id); StreamWriter file = new StreamWriter(arqNome); file.WriteLine("ID: " + Id); file.WriteLine("DOB: " + dOB); file.WriteLine("Name: " + name); file.WriteLine("Age: " + age);

Raised exception in the case of concurrent file access with StreamReader

半城伤御伤魂 提交于 2019-12-05 18:55:38
I found a post talking about handling concurrent file access with StreamWriter . The problem is that the answers do not manage the scenario where the file is being accessed but multiple processes. Let's tell it shortly : I have multiple applications I need a centralised logging system in dataBase If database fail, I need a fallback on a file system log There is a known concurrency scenario, where multiple applications (processes) will try to write in that file. This can be managed by re-attempt the writing after a short delay. But I don't want ot reattempt if it's a security error or filename

How To Overwrite A File If It Already Exists?

你。 提交于 2019-12-05 12:38:37
问题 I'm making a music player. It has 2 forms; one is the main area where you play music. The second form has a CheckedListBox where you select the mp3s you want. When I click a button, it saves the selection in a .txt file so I can access them in the first form, where I'll put the strings into the paths for music player to find the files. This is the code in my second form, where I save the selected songs into .txt files. private void selectbtn_Click(object sender, EventArgs e) { if (File.Exists

Writing to MemoryStream with StreamWriter returns empty

房东的猫 提交于 2019-12-05 09:14:01
问题 I am not sure what I am doing wrong, have seen a lot of examples, but can't seem to get this working. public static Stream Foo() { var memStream = new MemoryStream(); var streamWriter = new StreamWriter(memStream); for (int i = 0; i < 6; i++) streamWriter.WriteLine("TEST"); memStream.Seek(0, SeekOrigin.Begin); return memStream; } I am doing a simple test on this method to try and get it to pass, but no matter what, my collection count is 0. [Test] public void TestStreamRowCount() { var stream