-
Deleting files
I'm having trouble with the following code:
private void button1_Click(object sender, System.EventArgs e)
{
string path="c:\\temp\\filetest.txt";
string lines = "First line.\r\nSecond line.\r\nThird line.";
System.IO.StreamWriter file = new System.IO.StreamWriter(path);
file.WriteLine(lines);
file.Close();
}
private void button2_Click(object sender, System.EventArgs e)
{
string path="c:\\temp\\filetest.txt";
File.Delete(path);
}
The code creates and deletes a file fine as long as only the program accesses the file. The problem is: I create the file then close the program. Open the created file with notepad. Close notepad and then rerun the file program and try to either create or delete the file I get the following error:
Additional information: The process cannot access the file "c:\temp\filetest.txt" because it is being used by another process.
I ran Sysinternal's ProcessExplorer and there were no other processes using this text file. Even after rebooting the computer I get this error.
Any help on this is greatly appreciated.
-
your code works fine for me.
Code:
private void button1_Click(object sender, System.EventArgs e)
{
string path="c:\\temp\\filetest.txt";
string lines = "First line.\r\nSecond line.\r\nThird line.";
StreamWriter file=new StreamWriter(path);
file.WriteLine(lines);
file.Close();
}
private void button2_Click(object sender, System.EventArgs e)
{
string path="c:\\temp\\filetest.txt";
File.Delete(path);
}