System.Diagnostics.Process.Start();
这个方法用来创建一个新的进程 比如说你在某个程序的菜单上想加个超链接到百度知道 那么就可以用这个方法来实现 System.Diagnostics.Process.Start("iexplore.exe","http://zhidao.baidu.com"); 前面的第一个参数可以省略 但是如果这之前你有打开的网页 那么就会从那个网页跳转到 http://zhidao.baidu.com 没有打开的话 就打开一个新窗口
当然还可以用它来打开本地的一个程序 比如说点一个按钮就打开注册表编辑器的话 就应该是
System.Diagnostics.Process.Start(@"C:\WINDOWS\regedit.exe");

-----------------------------------------------------------------------------------------------------------------------------------

 

.NET 中 System.Diagnostics.Process 类应用中碰到的问题

 

自己开发的客户端程序需要从数据库的Image字段创建一个Word文档,然后调用Word程序打开,用户编辑并保存后再回存到数据库里。其中调用Word打开文档用的是System.Diagnostics.Process类,相关代码如下:
net中System.Diagnostics.Process.Start用法string tempPath = System.Environment.GetEnvironmentVariable("TEMP");
net中System.Diagnostics.Process.Start用法
string fileName = Path.Combine ( tempPath, "01.doc"
);
net中System.Diagnostics.Process.Start用法
net中System.Diagnostics.Process.Start用法Process wordProcess
= new
Process();
net中System.Diagnostics.Process.Start用法wordProcess.StartInfo.FileName
=
fileName;
net中System.Diagnostics.Process.Start用法wordProcess.StartInfo.Verb
= "edit"
;
net中System.Diagnostics.Process.Start用法wordProcess.StartInfo.UseShellExecute
= true
;
net中System.Diagnostics.Process.Start用法wordProcess.Start();
net中System.Diagnostics.Process.Start用法wordProcess.WaitForExit();
net中System.Diagnostics.Process.Start用法wordProcess.Close();
net中System.Diagnostics.Process.Start用法
net中System.Diagnostics.Process.Start用法MessageBox.Show (
"Word Exited!" );


前几天一直好用,但是今天在执行到 wordProcess.WaitForExit();这一句时发生意外,信息是“没有与此对象关联的进程”。仔细查看MSDN里的相关文档,也没有太详细的解释,只是在WaitForExit()方法的文档提到可能出现的意外中就包含了这个。突然想到有没有可能是因为Word进程已经打开,所以并不是和当前进程所关联的,所以会出现这个意外。关掉运行的其他Word进程以后,再执行果然就没有问题了。

经过一番研究,终于找到了有没有Word实例运行的情况下都能实现上一篇文章中的要求的方法,代码如下:

net中System.Diagnostics.Process.Start用法            string tempPath = System.Environment.GetEnvironmentVariable("TEMP");
net中System.Diagnostics.Process.Start用法            
string fileName = Path.Combine ( tempPath, "推理01.doc"
);
net中System.Diagnostics.Process.Start用法            
string winwordPath = ""
;
net中System.Diagnostics.Process.Start用法
net中System.Diagnostics.Process.Start用法            
// 判断系统中是否已经有 Word 实例在运行。

net中System.Diagnostics.Process.Start用法
             Process[] wordProcesses = Process.GetProcessesByName("winword");
net中System.Diagnostics.Process.Start用法            
foreach ( Process process in
wordProcesses)
net中System.Diagnostics.Process.Start用法net中System.Diagnostics.Process.Start用法          
{
net中System.Diagnostics.Process.Start用法                 Debug.WriteLine( process.MainWindowTitle );
net中System.Diagnostics.Process.Start用法                 winwordPath
= process.MainModule.FileName;        // 如果有的话获得 Winword.exe 的完全限定名称。

net中System.Diagnostics.Process.Start用法
                break;
net中System.Diagnostics.Process.Start用法             }

net中System.Diagnostics.Process.Start用法
net中System.Diagnostics.Process.Start用法             Process wordProcess
= new Process();
net中System.Diagnostics.Process.Start用法
net中System.Diagnostics.Process.Start用法            
if ( winwordPath.Length > 0 )    // 如果有 Word 实例在运行,使用 /w 参数来强制启动新实例,并将文件名作为参数传递。

net中System.Diagnostics.Process.Start用法net中System.Diagnostics.Process.Start用法
          {
net中System.Diagnostics.Process.Start用法                 wordProcess.StartInfo.FileName
=
winwordPath;
net中System.Diagnostics.Process.Start用法                 wordProcess.StartInfo.UseShellExecute
= false
;
net中System.Diagnostics.Process.Start用法                 wordProcess.StartInfo.Arguments
= fileName + " /w"
;
net中System.Diagnostics.Process.Start用法             }

net中System.Diagnostics.Process.Start用法            
else                            // 如果没有 Word 实例在运行,还是
net中System.Diagnostics.Process.Start用法net中System.Diagnostics.Process.Start用法
         {
net中System.Diagnostics.Process.Start用法                 wordProcess.StartInfo.FileName
=
fileName;
net中System.Diagnostics.Process.Start用法                 wordProcess.StartInfo.UseShellExecute
= true
;
net中System.Diagnostics.Process.Start用法             }

net中System.Diagnostics.Process.Start用法
net中System.Diagnostics.Process.Start用法             wordProcess.Start();
net中System.Diagnostics.Process.Start用法             wordProcess.WaitForExit();        
// 当前进程一直在等待,直到该 Word 实例退出。
net中System.Diagnostics.Process.Start用法
             wordProcess.Close();

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
  • 2021-11-13
相关资源
相似解决方案