2015-11-12 74 views
0

我正在處理我的第一個C#應用程序。我正嘗試以全屏模式打開PowerPoint文件。該代碼需要cmd參數。我將我的powerpoint test.pptm放在與我的應用程序的輸出(調試和發佈)相同的文件夾中。我寫了下面的代碼:嘗試使用C打開PowerPoint文件時拋出異常#

 ProcessStartInfo startInfo = new ProcessStartInfo(); 
     startInfo.CreateNoWindow = false; 
     startInfo.UseShellExecute = false; 
     startInfo.FileName = "powerpnt.exe"; 
     startInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     startInfo.Arguments = "/s test.pptm"; 

     try 
     { 
      using (Process exeProcess = Process.Start(startInfo)) 
      { 
       exeProcess.WaitForExit(); 
      } 
     } 
     catch 
     { 
     } 

代碼編譯,但是當我試圖通過一個按鈕,運行此代碼的控制檯狀態:

Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll

我試圖直接引用通過更改以下行PPTM文件:

startInfo.Arguments = "/s c:\path\to\full\file\test.pptm";

我得到一個錯誤,說明Unrecognized escape sequence。有誰之前經歷過這個嗎?我一直堅持這一點。謝謝!

+1

可以試一下這個請爲我行了? 'startInfo.Arguments =「/ s」「c:\ path \ to \ full \ file \ test.pptm」「」;' –

+0

@PauloLima謝謝,讓我放完整路徑。但主要問題仍然存在,即拋出異常:System.dll中的'System.ComponentModel.Win32Exception'。該程序似乎有適當的命令需要,但它不能執行它。 –

+1

看看[這篇文章](http://stackoverflow.com/questions/27596215/c-sharp-code-wont-launch-programs-win32exception-was-unhandled)可以幫助你 –

回答

4

前綴爲@符號的文件路徑

startInfo.Arguments = @"/s c:\path\to\full\file\test.pptm"; 

從MSDN

逐字字符串由@字符後跟一個雙引號字符,零個或多個字符,一個封閉的雙引號字符。一個簡單的例子是@「你好」。在逐字字符串文字中,分隔符之間的字符是逐字解釋的,唯一的例外是引號轉義序列。

https://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx

關於驗證碼的幾個指針得到它的工作

ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.CreateNoWindow = false;    
startInfo.FileName = "powerpnt.exe"; 
startInfo.WindowStyle = ProcessWindowStyle.Hidden; 
startInfo.Arguments = @"/s ""fullpath with spaces in file names"""; 
  1. 通知轉義雙引號之前和之後FULLPATH。這是爲了適應空間在文件名或目錄
  2. 刪除startInfo.UseShellExecute =假
+1

@cubrr注意。放在解釋中 –

+0

非常感謝!它現在似乎在工作。問題是'startInfo.UseShellExecute = false'行。這會導致我的異常錯誤。 –

相關問題