streamwriter

Does a Stream get Disposed when returning a File from an Action? [duplicate]

ぐ巨炮叔叔 提交于 2019-12-03 16:51:55
问题 This question already has answers here : Does FileStreamResult close Stream? (1 answer) How do I dispose my filestream when implementing a file download in ASP.NET? (2 answers) Closed 2 years ago . I'm writing a string to a MemoryStream I need to return the stream to the Controller Action so I can send it off as a file for download. Normally, I wrap the Stream in a using statement, but, in this case, I need to return it. Does it still get Disposed after I return it? Or do I need to dispose it

Can I write a file to a folder on a server machine from a Web API app running on it?

眉间皱痕 提交于 2019-12-03 12:24:25
I have this code in my Web API app to write to a CSV file: private void SaveToCSV(InventoryItem invItem, string dbContext) { string csvHeader = "id,pack_size,description,vendor_id,department,subdepartment,unit_cost,unit_list,open_qty,UPC_code,UPC_pack_size,vendor_item,crv_id"; int dbContextAsInt = 0; int.TryParse(dbContext, out dbContextAsInt); string csvFilename = string.Format("Platypus{0}.csv", dbContextAsInt); string csv = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}", invItem.ID, invItem.pksize, invItem.Description, invItem.vendor_id, invItem.dept, invItem.subdept

Write a string to a file

跟風遠走 提交于 2019-12-03 06:30:44
问题 I want to write something to a file. I found this code: private void writeToFile(String data) { try { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE)); outputStreamWriter.write(data); outputStreamWriter.close(); } catch (IOException e) { Log.e("Exception", "File write failed: " + e.toString()); } } The code seems very logical, but I can't find the config.txt file in my phone. How can I retrieve that file which includes

Does a Stream get Disposed when returning a File from an Action? [duplicate]

廉价感情. 提交于 2019-12-03 06:09:31
This question already has answers here : Does FileStreamResult close Stream? (1 answer) How do I dispose my filestream when implementing a file download in ASP.NET? (2 answers) I'm writing a string to a MemoryStream I need to return the stream to the Controller Action so I can send it off as a file for download. Normally, I wrap the Stream in a using statement, but, in this case, I need to return it. Does it still get Disposed after I return it? Or do I need to dispose it myself somehow? //inside CsvOutputFormatter public Stream GetStream(object genericObject) { var stream = new MemoryStream()

Do I need to do StreamWriter.flush()?

馋奶兔 提交于 2019-12-03 01:12:25
Suppose this C# code: using (MemoryStream stream = new MemoryStream()) { StreamWriter normalWriter = new StreamWriter(stream); BinaryWriter binaryWriter = new BinaryWriter(stream); foreach(...) { binaryWriter.Write(number); normalWriter.WriteLine(name); //<~~ easier to reader afterward. } return MemoryStream.ToArray(); } My questions are: Do I need to use flush inside the loop to preserve order? Is returning MemoryStream.ToArray() legal? I using the using -block as a convention, I'm afraid it will mess things up. Scratch the previous answer - I hadn't noticed that you were using two wrappers

How can I remove the oldest lines in a file when using a FileStream and StreamWriter?

[亡魂溺海] 提交于 2019-12-02 13:05:11
Based on Prakash's answer here , I thought I'd try something like this to remove the oldest lines in a file prior to adding a new line to it: private ExceptionLoggingService() { _fileStream = File.OpenWrite(GetExecutionFolder() + "\\Application.log"); _streamWriter = new StreamWriter(_fileStream); } public void WriteLog(string message) { const int MAX_LINES_DESIRED = 1000; StringBuilder formattedMessage = new StringBuilder(); formattedMessage.AppendLine("Date: " + DateTime.Now.ToString()); formattedMessage.AppendLine("Message: " + message); // First, remove the earliest lines from the file if

Streamwriter overwriting the text in txtfile

放肆的年华 提交于 2019-12-02 11:51:07
Is there some way to reopen streamwriter without it creating a new object to write to? Because at the moment, when WriteOdd is called, streamwriter is overwriting WriteEven, which is called before it :/ public void WriteEven() { StreamWriter writer = new StreamWriter(FILENAME); for (int i = 0; i < array.Length; i+= 2) { Console.WriteLine(array[i]); writer.WriteLine("EvenNumbers: " + array[i]); } writer.Close(); } public void WriteOdd() { StreamWriter writer = new StreamWriter(FILENAME); for (int i = 1; i < array.Length; i += 2) { Console.WriteLine(array[i]); writer.WriteLine("OddNumbers: " +

C# Program crashes when Writing using StreamWriter

蹲街弑〆低调 提交于 2019-12-02 10:20:30
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll It works only when the file is already created. When i delete the file and start from scratch it gives the following error Code: private void Btn_Create_Click(object sender, EventArgs e) { string path = Environment.CurrentDirectory + "/"+ "File.txt"; if (!File.Exists(path)) { File.CreateText(path); MessageBox.Show("File Created Successfully"); } else { MessageBox.Show("File Already Created"); } } private void Btn_Write_Click(object sender, EventArgs e) { using (StreamWriter sw = new StreamWriter("File.txt")) { sw

Can any one tell why the previous data is still displayed while saving data using StreamWriter

穿精又带淫゛_ 提交于 2019-12-02 09:45:23
I am saving data to a file-name using Stream writer but if i run the code for second time the same data is appended to the previous data but i would like to clear the old data and write the data The data i should have in text file should be as follows 101 435435345 3445454541104021031A094101 52251 1 1 CCD1 110402110402 1111000020000001 6281110000251 00000000011 1 1 0111000020000001 822500000100111000020000000000010000000000001 111000020000001 9000001000001000000010011100002000000000001000000000000 My sample code if (i == 0) { index++; string m_strDate = DateTime.Now.ToString("yyyy/MM/dd"); m

Self-closing StreamWriter singleton

落花浮王杯 提交于 2019-12-02 06:54:30
问题 I tried to further narrow down the problem in Flush StreamWriter at the end of its lifetime implementing a singleton-like self-closing StreamWriter : class Foo : System.IO.StreamWriter { private static readonly Foo instance = new Foo( "D:/tmp/test" ); private Foo( string path ) : base( path ) { } ~Foo() { this.Close(); } public static Foo Instance { get { return instance; } } } The intended effect is that in a sample program like class Program { private static void Main( string[] args ) { Foo