.NET 中 System.Diagnostics.Process 類應用中碰到的問題

來源:互聯網
上載者:User
自己開發的用戶端程式需要從資料庫的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!" );

前幾天一直好用,但是今天在執行到 wordProcess.WaitForExit();這一句時發生意外,資訊是“沒有與此對象關聯的進程”。仔細查看MSDN裡的相關文檔,也沒有太詳細的解釋,只是在WaitForExit()方法的文檔提到可能出現的意外中就包含了這個。突然想到有沒有可能是因為Word進程已經開啟,所以並不是和當前進程所關聯的,所以會出現這個意外。關掉啟動並執行其他Word進程以後,再執行果然就沒有問題了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.