,too...  I want to study

解决方案 »

  1.   

    利用FileInfo类将打开的文件共享模式设为
    None 谢绝共享当前文件。文件关闭前,打开该文件的任何请求(由此进程或另一进程发出的请求)都将失败。FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.None);
    然后定时打开文件如果失败说明还没有关闭。
      

  2.   

    FileInfo的例子using System;
    using System.IO;public class OpenTest 
    {
        public static void Main() 
        {
            // Open an existing file, or create a new one.
            FileInfo fi = new FileInfo("temp.txt");        // Open the file just specified such that no one else can use it.
            FileStream fs = fi.Open( FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None );        // Create another reference to the same file.
            FileInfo nextfi = new FileInfo("temp.txt");                try 
            {
                // Try opening the same file, which was locked by the previous process.
                nextfi.Open( FileMode.OpenOrCreate, FileAccess.Read );            Console.WriteLine("The file was not locked, and was opened by a second process.");
            } 
            catch (IOException) 
            {
                Console.WriteLine("The file could not be opened because it was locked by another process.");
            } 
            catch (Exception e) 
            {
                Console.WriteLine(e.ToString());
            }        // Close the file so it can be deleted.
            fs.Close();
        }
    }
      

  3.   

    songhtao(三十年孤独)
    如果采用隔段时间测试的方法,效率很低啊
    有没有其他的方法啊
      

  4.   

    下面的示例创建 FileSystemWatcher 来监视运行时指定的目录。组件设置为监视 LastWrite 和 LastAccess 时间方面的更改,以及目录中文本文件的创建、删除或重命名。如果更改、创建或删除文件,文件路径将被输出到控制台。在文件重命名后,旧路径和新路径都输出到控制台。public class Watcher
    {    public static void Main()
        {        string[] args = System.Environment.GetCommandLineArgs();
     
            // If a directory is not specified, exit program.
            if(args.Length != 2)
            {
                // Display the proper way to call the program.
                Console.WriteLine("Usage: Watcher.exe (directory)");
                return;
            }        // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = args[1];
            /* Watch for changes in LastAccess and LastWrite times, and 
               the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite 
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            // Only watch text files.
            watcher.Filter = "*.txt";        // Add event handlers.
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);        // Begin watching.
            watcher.EnableRaisingEvents = true;        // Wait for the user to quit the program.
            Console.WriteLine("Press \'q\' to quit the sample.");
            while(Console.Read()!='q');
        }    // Define the event handlers.
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
           Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
        }    private static void OnRenamed(object source, RenamedEventArgs e)
        {
            // Specify what is done when a file is renamed.
            Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
        }
    }
      

  5.   

    用FileSystemWatcher不能监视文件的打开和关闭啊