Results 1 to 2 of 2

Thread: Deleting files

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2003
    Posts
    5

    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.

  2. #2
    Lively Member ayan's Avatar
    Join Date
    Jan 2004
    Posts
    112
    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);
    		}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width