2

I am simply trying to create a file using c# and writing in it. Here is my code:

public void AddLog (string message, string fn_name)
{
    string path = @"E:\" + fn_name +".txt";
    if (!File.Exists(path))
    {
        File.Create(path);
        File.Delete(path);
        TextWriter tw = new StreamWriter(path);
        tw.WriteLine("" + message +"");
        tw.Close();
    }
    else if (File.Exists(path))
    {
        TextWriter tw = new StreamWriter(path,true);
        tw.WriteLine("" + message + "");
        tw.Close();
    }
}

The problem is it always gives me this exception:

{"The process cannot access the file 'E:\CheckForFriends.txt' because it is being used by another process."}

at this line: File.Delete(path); and even if I removed this line it gives me the same exception message at the line: File.Create(path);

So, anyone have an idea what is wrong in my code?

M. Schena
  • 2,039
  • 1
  • 21
  • 29
Abdallah
  • 523
  • 2
  • 7
  • 15
  • 1
    So if the file doesn't exist, you create it and then immediately delete it... why? – stuartd Feb 23 '16 at 10:32
  • Seriously, what are you trying to do?? – Viru Feb 23 '16 at 10:32
  • 1
    http://stackoverflow.com/questions/13335349/the-process-cannot-access-the-file-because-it-is-being-used-by-another-process – Ubiquitous Developers Feb 23 '16 at 10:33
  • @stuartd and @Viru i am trying to create a file and write in it...and even when i remove the `File.Delete(path);` still gives the same error at `TextWriter tw = new StreamWriter(path);` – Abdallah Feb 23 '16 at 10:47
  • Sorry, late to the party but 'real' problem with your code is that File.Create returns a FileStream object which needs to be disposed before trying to access the same file handle again. It's not as pretty as say, wrapping it in a using statement but you can chain the Dispose method on the call and clean it up in one fell swoop. ie: File.Create(path).Dispose(); – Mike Johnson Feb 09 '17 at 05:16
  • @MikeJohnson thank you...this makes sense – Abdallah Feb 21 '17 at 13:57

3 Answers3

5

When using disposable resources (things that implement IDisposable), you should use the using statement.

You can simplify to this, the StreamWriter will create the file if it doesn't exist.

public void AddLog (string message, string fn_name)
{
    string path = @"E:\" + fn_name +".txt";
    using(var tw = new StreamWriter(path, true))
    {
      tw.WriteLine(message);
    }
}
a-h
  • 4,244
  • 2
  • 23
  • 29
2

This is because you are creating and opening a file, and trying to delete it while it is still opened:

      File.Create(path);
      File.Delete(path);

It should be just

public void AddLog (string message, string fn_name)
    {
        string path = @"E:\" + fn_name +".txt";
        if (!File.Exists(path))
        {
            using (File.Create(path))
            using (TextWriter tw = new StreamWriter(path))
                tw.WriteLine("" + message +"");
        }
        else if (File.Exists(path))
        {
            using (TextWriter tw = new StreamWriter(path,true))
                tw.WriteLine("" + message + "");
        }
    }
0

i am not sure but you are deleting the file while it is being used by some other process:

try the below code:

public void AddLog (string message, string fn_name)
    {
        string path = @"E:\" + fn_name +".txt";
        if (!File.Exists(path))
        {
            File.Create(path);

            TextWriter tw = new StreamWriter(path);
            tw.WriteLine("" + message +"");
            tw.Close();
            File.Delete(path);

        }
        else if (File.Exists(path))
        {
            TextWriter tw = new StreamWriter(path,true);
            tw.WriteLine("" + message + "");
            tw.Close();
        }
    }
Ram Singh
  • 6,664
  • 35
  • 100
  • 166
  • I tried it still not working. It gives me the same error at: `TextWriter tw = new StreamWriter(path);` – Abdallah Feb 23 '16 at 10:38