1

I know File.Open() internally calls new FileStream(). So why does code utilizing File.Open() throw an IOException with the error message:

The process cannot access the file because it is being used by another process

Code with using FileStream() does not cause this error.

for (int i = 0; i < 20000; i++)
{
    XmlSerializer x1 = new XmlSerializer(typeof(Tasks));
    using (FileStream fileStream = File.Open(
             Settings.Default.DownloadJobFileName,
             FileMode.Open, 
             FileAccess.Read))
        {
            tasks = ((Tasks)xmlSerializer.Deserialize(fileStream));
        }

    XmlSerializer x2 = new XmlSerializer(typeof(Tasks));
    using (FileStream fileStream = File.Open(
              Settings.Default.DownloadJobFileName,
              FileMode.Create, 
              FileAccess.Write))
        {
            xmlSerializer.Serialize(fileStream, tasks);
        }
}

I'm using Windows 8.1 and .Net 4.0 client profile, seems it's getting worse on Windows Embedded Standard 2009

Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
alloha
  • 133
  • 1
  • 13
  • 2
    Can you post the code that uses `new FileStream()`? – Thomas Levesque Jul 16 '14 at 11:10
  • This question has been asked before, check out this link. http://stackoverflow.com/questions/4680284/system-io-file-create-locking-a-file – Brandon Jul 16 '14 at 11:10
  • @BrandonJ, what makes you think this question is related? To me it seems a completely different problem... – Thomas Levesque Jul 16 '14 at 11:11
  • @ThomasLevesque sure: using (FileStream fileStream = new FileStream(Settings.Default.DownloadJobFileName, FileMode.Create, FileAccess.Write)) and using (FileStream fileStream = new FileStream(Settings.Default.DownloadJobFileName, FileMode.Open, FileAccess.Read)) – alloha Jul 16 '14 at 11:24

1 Answers1

3

The FileShare options of the constructors/methods you use are different:

public FileStream(String path, FileMode mode, FileAccess access) 
    : this(path, mode, access, FileShare.Read,
           DefaultBufferSize, FileOptions.None, Path.GetFileName(path), false) {
}

compared to

public static FileStream Open(String path, FileMode mode, FileAccess access) {
    return Open(path,mode, access, FileShare.None);
}

public static FileStream Open(String path, FileMode mode, FileAccess access, FileShare share) {
    return new FileStream(path, mode, access, share);
}

When you create a new FileStream other processes can still read the file. File.Open on the other hand locks the file so no other process can access it.

Dirk
  • 10,668
  • 2
  • 35
  • 49
  • seems like correct answer, but I still don't understand why Dispose on non shared FileStream does not unlock file – alloha Jul 16 '14 at 12:18
  • @alloha It's certainly strange. Maybe you have the file open in another application, some editor for example. There are some tools which can be used to find which the process, e.g. http://www.codeproject.com/KB/shell/OpenedFileFinder.aspx?fid=422864&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=2277170. (I just found that one first when searching, I haven't tested it) – Dirk Jul 16 '14 at 12:29