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进程以后,再执行果然就没有问题了。