streamwriter

How to make a “Read only” file?

寵の児 提交于 2019-11-30 03:14:12
问题 I'm using the C# StreamWritier class. Questions: How can I make a file read-only, so that no one can delete or write to it? How can I make a hidden file? I'm creating the file like so: private void button1_Click(object sender, EventArgs e) { SaveFileDialog save = new SaveFileDialog(); save.FileName = textBox1.Text; save.Filter = "Text File | *.rtf"; if (save.ShowDialog() == DialogResult.OK) { StreamWriter writer = new StreamWriter(save.OpenFile()); writer.WriteLine(textBox2.Text); } writer

FileStream vs/differences StreamWriter?

試著忘記壹切 提交于 2019-11-29 19:14:05
Question: What is different between FileStream and StreamWriter in dotnet? What context are you supposed to use it? What is their advantage and disadvantage? Is it possible to combine these two into one? What is different between FileStream and StreamWriter in dotnet? A FileStream is a Stream . Like all Streams it only deals with byte[] data. A StreamWriter : TextWriter , is a Stream-decorator. A TextWriter encodes Text data like string or char to byte[] and then writes it to the linked Stream . What context are you supposed to use it? What is their advantage and disadvantage? You use a bare

StreamWriter limit in C# in text file

会有一股神秘感。 提交于 2019-11-29 13:56:39
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(maintNode); foreach (var line in chartList) { sw.WriteLine(line); } fs.Close(); Console.WriteLine("Done")

What is the default buffer size for StreamWriter

自作多情 提交于 2019-11-29 11:25:07
问题 For the public StreamWriter(Stream stream) constructor, MSDN says Initializes a new instance of the StreamWriter class for the specified stream by using UTF-8 encoding and the default buffer size. I want to use one of the other constructor overloads but would like to use the default buffer size. What is the default buffer size? MSDN does not say anywhere. Rubens Farias' answer here says it's "4 KiB" (whatever that means...KB I guess?) but there's no link to substantiate that. 回答1: Ah when

how to overwrite data in a txt file? [duplicate]

瘦欲@ 提交于 2019-11-29 10:58:04
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Can any one tell why the previous data is still displayed while saving data using StreamWriter I have WPF C# application, that reads and writes to a .txt file, i know how to write line but line, but how do I overwrite the text that is already the file. This is what I have just to write to the next line of the text file, but I want to over the lines not just write to the next line, thanks. using (StreamWriter

Try/Finally block vs calling dispose?

我的梦境 提交于 2019-11-29 09:23:41
问题 Is there any difference between these two code samples and if not, why does using exist? StreamWriter writer; try { writer = new StreamWriter(...) writer.blahblah(); } finally { writer.Dispose(); } vs: using (Streamwriter writer = new Streamwriter(...)) { writer.blahblah } I mean in the second example you really should be putting it in a try block anyway so adding the finally block really doesn't use much more effort. I get that the entire thing might be encompassed in a bigger try block but

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

妖精的绣舞 提交于 2019-11-29 09:11:56
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 illustrate the stream's properties. EDIT Expanding on the answer below, the solution is: var fs = new

Append text using StreamWriter [duplicate]

怎甘沉沦 提交于 2019-11-29 02:51:05
This question already has an answer here: Append lines to a file using a StreamWriter 10 answers This is probably a pretty simple question. In C# I'm trying to write a simple method, called my "DebugWrite" method, to write out any exceptions caught within my program to a text file stored locally. My current code only writes a new file every time, using StreamWriter How do you program it to check if the file already exists, and if so to append to the current text?. IE: If(~Exist(debug.txt) { Write new debug.txt. } else if(exist(debug.txt)) { Append new text. } using(StreamWriter writer = new

What consumes less resources and is faster File.AppendText or File.WriteAllText storing first in StringBuilder?

风格不统一 提交于 2019-11-29 02:18:42
I have to write thousands of dynamically generated lines to a text file. I have two choices, Which consumes less resources and is faster than the other? A. Using StringBuilder and File.WriteAllText StringBuilder sb = new StringBuilder(); foreach(Data dataItem in Datas) { sb.AppendLine( String.Format( "{0}, {1}-{2}", dataItem.Property1, dataItem.Property2, dataItem.Property3)); } File.WriteAllText("C:\\example.txt", sb.ToString(), new UTF8Encoding(false)); B. Using File.AppendText using(StreamWriter sw = File.AppendText("C:\\example.txt")) { foreach (Data dataItem in Datas) { sw.WriteLine(

DataGridView to CSV File

此生再无相见时 提交于 2019-11-28 22:06:10
I have a VB 2010 Express Project, that has a DataGridView in it, that I am trying to write to a CSV file. I have the write all working. But its slow. Slow = maybe 30 seconds for 6000 rows over 8 columns. Here is my code: Private Sub btnExportData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportData.Click Dim StrExport As String = "" For Each C As DataGridViewColumn In DataGridView1.Columns StrExport &= """" & C.HeaderText & """," Next StrExport = StrExport.Substring(0, StrExport.Length - 1) StrExport &= Environment.NewLine For Each R As DataGridViewRow In