Hi I tried create and write a text file in C# but I get this error.
The process is being used by another process 'D:\SampleFolder\Sample.txt' can not access the file.
public void WriteToLog(string ClassName, string Message)
{
string path = @"D:\SampleFolder\" + ClassName + ".txt";
if (!File.Exists(path))
{
File.Create(path);
TextWriter tw = new StreamWriter(path,true);
tw.WriteLine(DateTime.Now.ToString() + " " + "Class:" + " " + ClassName + " " + "Message:" + Message);
tw.Close();
}
else if (File.Exists(path))
{
TextWriter tw = new StreamWriter(path, true);
tw.WriteLine(DateTime.Now.ToString() + " " + "Class:" + " " + ClassName + " " + "Message:" + Message);
tw.Close();
}
}
..
..
try
{
}
catch (Exception ex)
{
WriteToLog(this.GetType().Name, ex.Message);
}