2011-02-06 66 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()方法与完整的文件路径。它会自动搜索相应的默认程序来打开文件。

相关问题