The file creates, but cannot write in it

My program: Check the Settings.txt file. If the file is missing, create the text and write it down automatically. If the Settings.txt file is already present, ignore it. Do not create or write to an existing file.

My problem: If the file is missing, the Settings.txt file is created, but it is empty. I want the program to write to it when it creates a file. Thank you for your help.

private void Form1_Load(object sender, EventArgs e) { string path = @"C:\Users\Smith\Documents\Visual Studio 2010\Projects\Ver.2\Settings.txt"; if (!File.Exists(path)) { File.Create(path); TextWriter tw = new StreamWriter(path); tw.WriteLine("Manual Numbers="); tw.WriteLine(""); tw.WriteLine("Installation Technical Manual: "); tw.WriteLine("Performance Manual: "); tw.WriteLine("Planned Maintenance Technical Manual: "); tw.WriteLine("Service Calibration Manual: "); tw.WriteLine("System Information Manual: "); tw.WriteLine(""); tw.Close(); } } 
+4
source share
4 answers

That's what I think. when I copied and ran your code, an exception was thrown. This is probably due to the fact that you create your file twice and do not close it before creating it a second time.

For reference, TextWriter tw = new StreamWriter(path); creates a file for you. You do not need to call File.Create

and during subsequent runs, I don’t think you are deleting the file, and since the file already exists, if (!File.Exists(path)) will never be satisfied and the whole if will be skipped

So there are a few points

  • get rid of this call to File.Create
  • If you want the file to be overwritten, you should not check if it exists, you should simply overwrite.
+5
source

Try the following:

  using(FileStream stream = File.Create(path)) { TextWriter tw = new StreamWriter(stream); tw.WriteLine("Manual Numbers="); tw.WriteLine(""); tw.WriteLine("Installation Technical Manual: "); tw.WriteLine("Performance Manual: "); tw.WriteLine("Planned Maintenance Technical Manual: "); tw.WriteLine("Service Calibration Manual: "); tw.WriteLine("System Information Manual: "); tw.WriteLine(""); } 

Use ensures that the filter stream is closed (deleted), even if an exception occurs in the message.

+14
source

The problem is that File.Create returns a FileStream, so it leaves the file open. You must use this FileStream with your TextWriter. You will also want to use a FileStream in a using (...) statement or manually call Dispose () on a FileStream to make sure the file is closed when you finish processing it.

+8
source

although I answer after a while, but I think I should answer

 using (TextWriter tw = new StreamWriter(path)) { StringBuilder sb = new StringBuilder(); sb.Append("Manual Numbers="); sb.Append(Environment.NewLine); sb.Append("Installation Technical Manual: "); sb.Append("Performance Manual: "); sb.Append("Planned Maintenance Technical Manual: "); sb.Append("Service Calibration Manual: "); sb.Append("System Information Manual: "); sb.Append(Environment.NewLine); tw.Write(sb.ToString()); } 
+2
source

Source: https://habr.com/ru/post/1499234/


All Articles