streamwriter

Forcing StreamWriter to change Encoding

你离开我真会死。 提交于 2019-11-28 20:16:34
I am trying to save a file using DialogResult and StringBuilder . After making the text, I am calling the following code to save the file: if (dr == DialogResult.OK) { StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); sw.Write(sb.ToString()); sw.Close(); } I tried to add the second parameter to StreamWriter as Encoding.UTF8 but since the first argument is a string rather than a Stream , it does not compile it. How can I convert that string to a stream to be able to pass the second parameter as Encoding? The reason for this, is that somewhere in my text I have µ but when the file is

Create File If File Does Not Exist

痴心易碎 提交于 2019-11-28 16:47:47
I need to get my code to read if file doesnt exist create else append. Right now it is reading if it does exist create and append. Here is the code: if (File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { Would I do this? if (! File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { Edit: string path = txtFilePath.Text; if (!File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { foreach (var line in employeeList.Items) { sw.WriteLine(((Employee)line).FirstName); sw.WriteLine(((Employee)line).LastName); sw.WriteLine(((Employee)line).JobTitle); }

Writing to file using StreamWriter much slower than file copy over slow network

吃可爱长大的小学妹 提交于 2019-11-28 14:05:02
I have a program that attempts to write quite a large amount of text to a file on a remote server overseas, which has a slow network connection. Using the following code, where outputFileContent is a StringBuilder : using (var outfile = new StreamWriter(myRemoteFilePath)) { outfile.Write(outputFileContent.ToString()); } it is taking a seriously long time to run (several minutes), whereas if I first write to a local file and then copy it across to the remote location, it is much quicker (20-30 secs): string tempFilePath = Path.GetTempFileName(); using (var outfile = new StreamWriter

NodeJS write binary buffer into a file

好久不见. 提交于 2019-11-28 10:45:37
I can't rewrite a file that I am getting from a binary buffer, I have checked with the original file and all bytes are the same. This is the file create from NodeJS: # hd test.txt | head 00000000 47 49 46 38 39 61 32 00 32 00 f7 00 00 96 8c 73 |GIF89a2.2.�....s| 00000010 66 5e 45 c6 bb 9f 7b 72 5a 47 47 47 8a 81 65 ca |f^Eƻ.{rZGGG..e�| 00000020 c1 a6 c9 c1 ac ee ea dd c8 c5 bc 8c 87 7a d3 c9 |���������ż..z��| 00000030 ab 43 3b 26 eb e5 d1 fa fa fa e5 e4 e2 a6 9d 83 |�C;&����������..| 00000040 86 7e 67 c1 b4 8e e4 dc c6 82 82 82 e1 dd d1 e3 |.~g��.���...����| 00000050 dd ca e4 da bc f5 f1 e6 26

how to use StreamWriter class properly?

佐手、 提交于 2019-11-28 09:20:25
I am using StreamWriter class for file operations, are there any problems in this code that I am not seeing? Such as do I need to put it into a try catch finally block? StreamWriter sr = new StreamWriter(streamFolder); sr.Write(details); File.SetAttributes(streamFolder, FileAttributes.Hidden); sr.Close(); Whats wrong with your code? If some exception will occur before you close stream, then stream will stay open, and system resources will not be released: StreamWriter sr = new StreamWriter(streamFolder); sr.Write(details); // some exception occurs here File.SetAttributes(streamFolder,

how to read special character like é, â and others in C#

冷暖自知 提交于 2019-11-28 08:07:14
I can't read those special characters I tried like this 1st way # string xmlFile = File.ReadAllText(fileName); 2nd way # FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader r = new StreamReader(fs); string s = r.ReadToEnd(); But both statements don't understand those special characters. How should I read? UPDATE ### I also try all encoding with string xmlFile = File.ReadAllText(fileName, Encoding. ); but still don't understand those special characters. There is no such thing as "special character". What those likely are is extended ascii characters from the

StreamWriter limit in C# in text file

十年热恋 提交于 2019-11-28 07:54:16
问题 I have an array list which contains 100 lines. When i try to export it into a text file (txt), the output is only 84 lines and it stops in the middle of the 84th line. When I looked at the file size it showed exactly sharp 4.00KB as if there is some kind of a limit to the stream writer. I tried using different parameters etc. but it kept happening. Here is the code: FileStream fs = new FileStream(path, FileMode.Create); StreamWriter sw = new StreamWriter(fs); ArrayList chartList = GetChart

StreamWriter Multi Threading C#

巧了我就是萌 提交于 2019-11-28 05:30:51
问题 I would like to ask help on my code. I am a newbie and wanted to implement safe multi threading in writing to a text file. StreamWriter sw = new StreamWriter(@"C:\DailyLog.txt"); private void Update(){ var collection = Database.GetCollection<Entity>("products"); StreamReader sr = new StreamReader(@"C:\LUSTK.txt"); string[] line = sr.ReadLine().Split(new char[] { ';' }); while (!sr.EndOfStream) { line = sr.ReadLine().Split(new char[] { ';' }); t = delegate { UpdateEach(Convert.ToInt32(line[5])

FileStream and StreamWriter - How to truncate the remainder of the file after writing?

喜欢而已 提交于 2019-11-28 02:34:46
问题 var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); using(var writer = new StreamWriter(fs)) writer.Write(....); If the file previously contained text and the newly-written text is shorter than what was already in the file, how do I make sure that the obsolete trailing content in the file is truncated? Note that opening the file in truncate mode isn't an option in this case. The file is already open when I receive the FileStream object. The above code is just to

StreamWriter replace line with a new text

放肆的年华 提交于 2019-11-27 18:43:09
问题 Is it possible to replace the text in a text file with a new text without erasing the other data, here is my sample code, but its not working, I know there's a problem with it but I can't figure out, thanks, private void button1_Click_1(object sender, EventArgs e) { StreamReader sr = new StreamReader("test10101.txt"); List<string> lines = new List<string>(); while (!sr.EndOfStream) lines.Add(sr.ReadLine()); output = Convert.ToInt32(textBox1.Text); newbal = Convert.ToInt32(lines[0]) - output;