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来决定
}
谢谢你回答了我的问题,你的这种方法是把进程的结束当做了文件的关闭,这对于notepad是可行的。
如果用的不是notepad,而是Uedit该怎么办呢?一个Uedit进程里可以打开多个文件,如何识别其中一个文件的关闭呢?
ps:UE是打读完就关闭,记事本也这样。
CallingConvention=CallingConvention.StdCall)]
public static extern IntPtr FindWindow(string lpclass,string lpcap);if(FindWindow("","文档.txt - 记事本")==IntPtr.Zero)
{
//已经关闭了你在程序中打开的记事本,加入处理代码
}
{ 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);
}
}