streamwriter

How to use StreamWriter.WriteAsync and catch exceptions?

六月ゝ 毕业季﹏ 提交于 2019-12-24 15:17:40
问题 I have simple function to write files. public static void WriteFile(string filename, string text) { StreamWriter file = new StreamWriter(filename); try { file.Write(text); } catch (System.UnauthorizedAccessException) { MessageBox.Show("You have no write permission in that folder."); } catch (System.Exception e) { MessageBox.Show(e.Message); } file.Close(); } How can I convert my code to use StreamWriter.WriteAsync with try-catch? 回答1: async public static void WriteFile(string filename, string

Closing Stream Read and Stream Writer when the form exits

喜夏-厌秋 提交于 2019-12-24 10:58:04
问题 Hey guys, having a bit of trouble here. So I have a load button that loads the file, and a save button the saves the file. I also have a exit button that closes the program. What I need help with is when I close the program, I wont to check whether there are any StreamReader or StreamWriter things that have not been closed. Heres what I have so far: At the top, i declear these guys bool isDirtyBoolean = false; string moreData = ""; My load button looks like this private void

Writing Text File to Folder on Desktop

房东的猫 提交于 2019-12-24 08:39:28
问题 I'm wondering how do you write a text file to a specific location on the desktop such as C:\Users\tommy\desktop\transaction\ using StreamWriter? private void CreateTransaction_Click(object sender, EventArgs e) { StreamWriter outputFile; outputFile = File.CreateText (TID.ToString()+".txt"); outputFile.WriteLine("Investor :" +" " + InvestorNameLabel.Text); outputFile.WriteLine("Initial Amount" + " " +AmountLabel.Text); outputFile.WriteLine("Date Invested" +" " +DateLabel.Text); outputFile

Write a text file to a sub-folder

萝らか妹 提交于 2019-12-24 07:03:26
问题 I am trying to write out a text file to: C:\Test folder\output\ , but without putting C:\ in. i.e. This is what I have at the moment, which currently works, but has the C:\ in the beginning. StreamWriter sw = new StreamWriter(@"C:\Test folder\output\test.txt"); I really want to write the file to the output folder, but with out having to have C:\ in the front. I have tried the following, but my program just hangs (doesn't write the file out): (@"\\Test folder\output\test.txt"); (@".\Test

Can't write to stream inside event

早过忘川 提交于 2019-12-24 04:30:19
问题 I am can try to run NHTTP(simple asynchronous HTTP server written in C# for the .NET framework. You can download this dll in nuget, or view source code on github) But i am cant write my own response to client from my server, because i am can't write to OutputStream. My powershell script: Add-Type -Path "NHttp.dll" $server = New-Object NHttp.HttpServer $server.EndPoint = new-object System.Net.IPEndPoint ([System.Net.IPAddress]::Parse("127.0.0.1"), 8080) $RequestReceived = { $request =

Writing to a file and handling duplicate entries

折月煮酒 提交于 2019-12-24 02:07:27
问题 BACKGROUND: 1 Button 1 File That Needs To Be Written To 1 TextBox 1 NumericUpDown So, in my application I need to write to a file that will contain several lines. Input is taken from a TextBox and a NumericUpDown control and it comprises a string of general format string.Format("{0}|{1}", TextBoxInput, NumericUpDownInput); . What I need help with is actually checking for duplicate entries before adding a new line. Basically, if the user decides to enter something they already had (to update

streamwriter declared static vs with an using statement

穿精又带淫゛_ 提交于 2019-12-24 00:26:46
问题 I'm VERY new to C# so please allow me some ignorance :) (I've tried searching around to understand the reason for the difference in performance I'm seeing but as of yet don't have a definitive answer so I thought I'd ask the knowledgable audience on here...) Basically... if I use streamwriter something like: public static class Logging { readonly static object DebugWriter = new object(); public static void Log(string msg) { lock (DebugWriter) { using (StreamWriter writer = new StreamWriter(

How does BufferedWriter work in java

孤人 提交于 2019-12-23 09:17:29
问题 I frequently output text to files. I wonder something: how does BufferedWriter work? Does it write text on file when I call writer.write(text) ? If it doesn't write text,do I need to use flush function to write data? For example: File file = new File("statistics.txt"); if (!file.exists()) { file.createNewFile(); } else { file.delete(); file.createNewFile(); } FileWriter fileWritter = new FileWriter(file.getAbsoluteFile(),true); BufferedWriter bufferWritter = new BufferedWriter(fileWritter);

How to create copy of file using StreamReader and StreamWriter

喜你入骨 提交于 2019-12-23 09:11:09
问题 I need to use StreamReader to read a .txt file on a console application, then create a new file or backup with a different name but same content. The problem is i cant figure out how to use the content from the first file to place into the new one. (This is for a school thing and im new to C#) using System; using System.IO; namespace UserListCopier { class Program { static void Main() { string fineName = "zombieList.txt"; StreamReader reader = new StreamReader(fineName); int lineNumber = 0;

StreamReader and Portable Class Library

天大地大妈咪最大 提交于 2019-12-23 07:19:31
问题 I am writing a ConfigManager class using Portable Class Libraries. PCL supports StreamReader and StreamWriter classes that I want to use, but the PCL version of those classes do not support passing in a string during construction. PCL also does not support the reader.Close() and writer.Close() . Lastly it doesn't support the FileStream class. So I am looking for an answer to any one of the following questions: How can I get the StreamReader and StreamWriter classes working in a PCL? How can I