File is being used by another process

后端 未结 7 1240
野的像风
野的像风 2021-01-11 21:08

I have a program that roughly does this:

  1. open a file to read from it.
  2. close the file
  3. Start a filewatcher to watch for changes in the file.
相关标签:
7条回答
  • 2021-01-11 21:38

    If your code is similar to this:

    [STAThread]
    static void Main(string[] args)
    {
        string file = "temp.txt";
        ReadFile(file);
        FileSystemWatcher fswatcher = new FileSystemWatcher(".\\");
        fswatcher.Changed += delegate(object sender, FileSystemEventArgs e)
                                 {
                                     ReadFile(e.FullPath);
                                 };
        while (true)
        {
            fswatcher.WaitForChanged(WatcherChangeTypes.Changed);
        }
    }
    private static void ReadFile(string file)
    {
        Stream stream = File.OpenRead(file);
        StreamReader streamReader = new StreamReader(stream);
        string str = streamReader.ReadToEnd();
        MessageBox.Show(str);
        streamReader.Close();
        stream.Close();
    }
    

    If you are editing the file via notepad, then, when you click the save button, it keeps the file open, while as if when you just close the program and click save it doesn't. I do no know if this is a bug or an undocumented feature of notepad, but this just might be your problem. One way to fix this is to do the following:

    In your anonymous delegate, or wherever you execute the call to ReadFile() call Thread.Sleep(1000), to have the program wait before reading the file and your code should work fine.

    0 讨论(0)
  • 2021-01-11 21:38

    Note: even if "file.txt" is open in Notepad, this code still works, because it is opening for read.

    using System;
    using System.IO;
    
    class Program
    {
      static void Main(string[] args)
      {
        ReadFromFile(@"C:\file.txt");
        Console.ReadLine();
      }
    
      static void ReadFromFile(string filename)
      {
        string line;
        using (StreamReader sr = File.OpenText(filename))
        {
          line  = sr.ReadLine();
          while (line != null)
          {
            Console.WriteLine(str);
            line = sr.ReadLine();
          }
          sr.Close();
        }
      }
    }
    

    Or just:

    string text = System.IO.File.ReadAllText(@"C:\file.txt");

    0 讨论(0)
  • 2021-01-11 21:42

    Beside other answers it is possible that when FileWatcher reacts file it not yet closed by that app. In step 1 try not to fail immediately but try several attempts with small delay.

    0 讨论(0)
  • 2021-01-11 21:54

    You can use a tool like Process Explorer from http://www.sysinternals.com to see who has the open handle to the process

    The file is most likely held open by whatever caused the change notification to fire in the first place.

    0 讨论(0)
  • 2021-01-11 21:55

    There are a number of things that could be going on.

    First, make sure you properly dispose of the file writer (close isn't good enough) by utilizing the using clause around everything that implements IDisposable.

    Second, it you are simply reading, make sure you have the correct flags set when opening the file.

    To go any further it would help if you provided a code block which showed how you were accomplishing this; particularly around the reading of the file

    0 讨论(0)
  • 2021-01-11 21:58

    You can use this MS utility openfiles to obtain list of opened files and understand who has opened the file.

    openfiles /query
    

    Also it allow to disconnect files opened by network users.

    openfiles /disconnect /id XXXX
    

    If you want use it for local PC you should set Maintain Objects List global flag:

    openfiles /local on
    

    Follow the link to get more details.

    0 讨论(0)
提交回复
热议问题