Process.WaitForExit()不能处理多个excel和word文件

本文关键字:excel word 文件 处理 WaitForExit 不能 Process | 更新日期: 2023-09-27 18:06:02

Process.WaitForExit()不工作,如果我打开多个excel表格,程序只是运行,但如果我只打开一个excel,那么它会击中Process.WaitForExit();线。

示例代码:

ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.FileName = "D:''test.xlsx";
                    startInfo.WindowStyle = ProcessWindowStyle.Maximized;
                    try
                    {
                        using (Process exeProcess = Process.Start(startInfo))
                        {
                            exeProcess.WaitForExit();
                            if (saveasUserReport)
                            {
                                DateTime reportUpdatedDateTime = File.GetLastWriteTime(outputPath);
                                if (reportUpdatedDateTime > reportCreationDateTime)
                                {
                                    DialogResult dialogResult = MessageBox.Show(NotificationHelper.GetDisplayText(BrandingTypes.Message, "Do you want to save?"),
                                        NotificationHelper.GetDisplayText(BrandingTypes.Message, "Reports"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                                    if (dialogResult == System.Windows.Forms.DialogResult.Yes) 
                                        saveReport = true;
                                }
                            }
                        }
                    }
                    catch
                    {
                    }

Process.WaitForExit()不能处理多个excel和word文件

System.IO。FileSystemWatcher捕获文件中的任何更改。一旦用户在excel中做了任何更改并保存它,它将触发一个事件。

private System.IO.FileSystemWatcher fsw;
fsw = new System.IO.FileSystemWatcher();
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                             | NotifyFilters.FileName | NotifyFilters.DirectoryName;
fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnChanged);
fsw.Deleted += new FileSystemEventHandler(OnChanged);
fsw.Renamed += new RenamedEventHandler(OnRenamed);
fsw.EnableRaisingEvents = true;