LaVOZs

The World’s Largest Online Community for Developers

'; c# - File creates but cannot write in it - LavOzs.Com

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

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.

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.

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.

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());
 }
Related
Create Generic method constraining T to an Enum
How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?
Creating a byte array from a stream
Writing file to web server - ASP.NET
Interop type cannot be embedded
How can I use OLEDB to populate listview from a .XLS file
Writing to output window of Visual Studio
Access and read data from multiple *.resx files