2011-02-06 69 views
1

我有從C# 繼啓動過程中的一些異常代碼系統無法找到文件過程中指定的異常啓動

Process myProcess = new Process(); 
try 
{ 
    myProcess.StartInfo.UseShellExecute = true; 
    myProcess.StartInfo.FileName = "c:\\windows\\system32\\notepad.exe C:\\Users\\Karthick\\AppData\\Local\\Temp\\5aau1orm.txt"; 
    myProcess.StartInfo.CreateNoWindow = false; 
    myProcess.Start(); 
} 
catch (Exception e) 
{ 
    Console.WriteLine(e.Message); 
} 

,有時我得到的異常「的文件名,目錄名,或卷標語法不正確」如果useShellExecute設置爲false

任何想法,這是爲什麼不出來正確

回答

1

正如@SLaks提到的,這是適當的方式 讓默認應用程序(在你的情況映射到.txt擴展名)打開文件

Process.Start("test.txt"); 

但是,如果你喜歡打開文本文件只在記事本中而不是其他默認文本編輯器

ProcessStartInfo processStartInfo = new ProcessStartInfo(@"c:\Windows\System32\notepad.exe", "text.txt"); 
Process.Start(processStartInfo); 
+0

謝謝。這工作正好。我只想要記事本打開應用程序 – Karthick 2011-02-07 01:28:28

3

你不能把一個完整的命令行中FileName財產。

相反,你應該只Start txt文件,將在用戶的默認編輯器中打開:

Process.Start(@"C:\Users\Karthick\AppData\Local\Temp\5aau1orm.txt"); 
1

您試圖執行c:\\windows\\system32\\notepad.exe C:\\Users\\Karthick\\AppData\\Local\\Temp\\5aau1orm.txt。如果你沒有使用shell,它將被逐字解釋。如果使用shell,那麼shell將負責參數分析。使用ProcessStartInfo.Arguements屬性來提供參數。

0

FileName屬性不能使用命令行類型的語法。你指定的是命令行。

由於它只有一個.txt文件,您可以使用Process.Start()方法與完整的文件路徑。它會自動搜索相應的默認程序來打開文件。

相關問題