tags:

views:

274

answers:

4

Hi All,

I'm trying to detect if a file exists at runtime, if not, create it. However I'm getting a 'The process cannot access the file 'myfile.ext' because it is being used by another process.' error when I try to write to it.

  string filePath = string.Format(@"{0}\M{1}.dat", ConfigurationManager.AppSettings["DirectoryPath"], costCentre); 
  if (!File.Exists(filePath)) { 
    File.Create(filePath); 
  } 
  using (StreamWriter sw = File.AppendText(filePath)) { 
    //write my text 
  }

Any ideas how to fix it?

Thanks

+1  A: 

Have you closed the file after creating it? Could you show the code you are using. When creating a text file you can use the following code:

System.IO.File.WriteAllText("c:\test.txt", "all of your content here");

Using the code from your comment. The file(stream) you created must be closed. File.Create return the filestream to the just created file.:

            string filePath = "filepath here";
        if (!System.IO.File.Exists(filePath))
        {
            System.IO.FileStream f = System.IO.File.Create(filePath);
            f.Close();
        }
        using (System.IO.StreamWriter sw = System.IO.File.AppendText(filePath))
        { 
            //write my text 
        }
rdkleine
I don't seem to have a close option. Here's the code: string filePath = string.Format(@"{0}\M{1}.dat", ConfigurationManager.AppSettings["DirectoryPath"], costCentre);if (!File.Exists(filePath)){File.Create(filePath);} using (StreamWriter sw = File.AppendText(filePath)){//write my text}
Brett
+2  A: 

The File.Create method creates the file and opens a FileStream on the file. So your file is already open. You don't really need the file.Create method at all:

string filePath = @"c:\somefilename.txt";
using (StreamWriter sw = new StreamWriter(filePath, true))
{
    //write to the file
}

The boolean in the StreamWriter constructor will cause the contents to be appended if the file exists.

Chris Dunaway
+1  A: 

File.Create returns a FileStream. You need to close that when you have written to the file:

using (FileStream fs = File.Create(path, 1024)) 
        {
            Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
            // Add some information to the file.
            fs.Write(info, 0, info.Length);
        }

You can use using for automatically closing the file.

kimtiede
+1  A: 

I updated your question with the code snippet. After proper indenting, it is immediately clear what the problem is: you use File.Create() but don't close the FileStream that it returns.

Doing it that way is unnecessary, StreamWriter already allows appending to an existing file and creating a new file if it doesn't yet exist. Like this:

  string filePath = string.Format(@"{0}\M{1}.dat", ConfigurationManager.AppSettings["DirectoryPath"], costCentre); 
  using (StreamWriter sw = new StreamWriter(filePath, true)) {
    //write my text 
  }

Which uses this StreamWriter constructor.

Hans Passant