Smith
Smith

Reputation: 407

File creates but cannot write in it

My Program: Check for Settings.txt file. If the file is not present, create the text and write into it automatically. If Settings.txt file is already present, ignore. Do not create or write in the existing file.

My Problem: When file is not present, Settings.txt file creates but it is empty. I want the program to write in it when it creates the file. Thanks 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();
        }
    }

Upvotes: 4

Views: 630

Answers (4)

Ehsan
Ehsan

Reputation: 32701

though i am answering after quite some time but i guess 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());
 }

Upvotes: 2

Here's what I think happened. when I copied and ran your code, an exception was thrown. This is probably becaue you create your file twice and don't close it before you create it the second time.

For reference, TextWriter tw = new StreamWriter(path); creates the file for you. You don't need to call File.Create

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

So there are multiple points here

  • get rid of that File.Create call
  • if you want the file to be over-written, you shouldn't check if it exists, you should just overwrite.

Upvotes: 6

chris.house.00
chris.house.00

Reputation: 3301

The problem is that File.Create returns a FileStream so it leaves the file open. You need to use that FileStream with your TextWriter. You'll also want to wrp the FileStream in a using(...) statement or manually call Dispose() on the FileStream so you ensure the file is closed when you're done processing it.

Upvotes: 8

Jeroen van Langen
Jeroen van Langen

Reputation: 22073

Try this:

    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("");
    }

The using ensures that the filestream is closed(disposed) even when an exception occurs within writing.

Upvotes: 14

Related Questions