System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName("notepad");
foreach(System.Diagnostics.Process p in ps)
{
p.EnableRaisingEvents = true;
p.Exited +=new EventHandler(p_Exited);
}
private void p_Exited(object sender, EventArgs e)
{
//到这里说明被关了,可以有多个notepad,具体取哪个可以由id来决定
}

解决方案 »

  1.   

    To:antoniusguo(anton)
    谢谢你回答了我的问题,你的这种方法是把进程的结束当做了文件的关闭,这对于notepad是可行的。
    如果用的不是notepad,而是Uedit该怎么办呢?一个Uedit进程里可以打开多个文件,如何识别其中一个文件的关闭呢?
      

  2.   

    你试一下Uedit看看关闭一个文件是不是也会触发该事件.
      

  3.   

    用一个死循环,一直试图打开这个文件。如果被别的打开了,就会丢出一个exception,如果成功打开,则跳出这个死循环。
      

  4.   

    都没有用的,我记得以前学操作系统的时候,老师曾经说过:在打开一个文件的时候操作系统会在某链表上加一个值,关闭一次就取掉一个(上课睡觉的时候听的,大概可能是这样),所以要知道是否打开也只能读这个表了吧(我猜),这个表在哪我就不知道了。
    ps:UE是打读完就关闭,记事本也这样。
      

  5.   

    [DllImport("user32.dll",CharSet=CharSet.Auto,
     CallingConvention=CallingConvention.StdCall)]
    public static extern IntPtr FindWindow(string lpclass,string lpcap);if(FindWindow("","文档.txt - 记事本")==IntPtr.Zero)
    {
    //已经关闭了你在程序中打开的记事本,加入处理代码
    }
      

  6.   

    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);
        }
    }