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?