-----------------------------------------------------------------------------------------------------------------------------------
.NET 中 System.Diagnostics.Process 类应用中碰到的问题
自己开发的客户端程序需要从数据库的Image字段创建一个Word文档,然后调用Word程序打开,用户编辑并保存后再回存到数据库里。其中调用Word打开文档用的是System.Diagnostics.Process类,相关代码如下:
string tempPath = System.Environment.GetEnvironmentVariable("TEMP"
);
string fileName = Path.Combine ( tempPath, "01.doc" );
Process wordProcess = new Process();
wordProcess.StartInfo.FileName = fileName;
wordProcess.StartInfo.Verb = "edit" ;
wordProcess.StartInfo.UseShellExecute = true ;
wordProcess.Start();
wordProcess.WaitForExit();
wordProcess.Close();
MessageBox.Show ( "Word Exited!" );
string fileName = Path.Combine ( tempPath, "01.doc" );
Process wordProcess = new Process();
wordProcess.StartInfo.FileName = fileName;
wordProcess.StartInfo.Verb = "edit" ;
wordProcess.StartInfo.UseShellExecute = true ;
wordProcess.Start();
wordProcess.WaitForExit();
wordProcess.Close();
MessageBox.Show ( "Word Exited!" );
前几天一直好用,但是今天在执行到 wordProcess.WaitForExit();这一句时发生意外,信息是“没有与此对象关联的进程”。仔细查看MSDN里的相关文档,也没有太详细的解释,只是在WaitForExit()方法的文档提到可能出现的意外中就包含了这个。突然想到有没有可能是因为Word进程已经打开,所以并不是和当前进程所关联的,所以会出现这个意外。关掉运行的其他Word进程以后,再执行果然就没有问题了。
经过一番研究,终于找到了有没有Word实例运行的情况下都能实现上一篇文章中的要求的方法,代码如下:
string tempPath = System.Environment.GetEnvironmentVariable("TEMP"
);
string fileName = Path.Combine ( tempPath, "推理01.doc" );
string winwordPath = "" ;
// 判断系统中是否已经有 Word 实例在运行。
Process[] wordProcesses = Process.GetProcessesByName("winword" );
foreach ( Process process in wordProcesses)
{
Debug.WriteLine( process.MainWindowTitle );
winwordPath = process.MainModule.FileName; // 如果有的话获得 Winword.exe 的完全限定名称。
break;
}
Process wordProcess = new Process();
if ( winwordPath.Length > 0 ) // 如果有 Word 实例在运行,使用 /w 参数来强制启动新实例,并将文件名作为参数传递。
{
wordProcess.StartInfo.FileName = winwordPath;
wordProcess.StartInfo.UseShellExecute = false;
wordProcess.StartInfo.Arguments = fileName + " /w";
}
else // 如果没有 Word 实例在运行,还是
{
wordProcess.StartInfo.FileName = fileName;
wordProcess.StartInfo.UseShellExecute = true;
}
wordProcess.Start();
wordProcess.WaitForExit(); // 当前进程一直在等待,直到该 Word 实例退出。
wordProcess.Close();
string fileName = Path.Combine ( tempPath, "推理01.doc" );
string winwordPath = "" ;
// 判断系统中是否已经有 Word 实例在运行。
Process[] wordProcesses = Process.GetProcessesByName("winword" );
foreach ( Process process in wordProcesses)
{
Debug.WriteLine( process.MainWindowTitle );
winwordPath = process.MainModule.FileName; // 如果有的话获得 Winword.exe 的完全限定名称。
break;
}
Process wordProcess = new Process();
if ( winwordPath.Length > 0 ) // 如果有 Word 实例在运行,使用 /w 参数来强制启动新实例,并将文件名作为参数传递。
{
wordProcess.StartInfo.FileName = winwordPath;
wordProcess.StartInfo.UseShellExecute = false;
wordProcess.StartInfo.Arguments = fileName + " /w";
}
else // 如果没有 Word 实例在运行,还是
{
wordProcess.StartInfo.FileName = fileName;
wordProcess.StartInfo.UseShellExecute = true;
}
wordProcess.Start();
wordProcess.WaitForExit(); // 当前进程一直在等待,直到该 Word 实例退出。
wordProcess.Close();