The system cannot find the file specified Exception in Process Start
I am having some exception in starting a process from C# Following is the cod开发者_StackOverflow社区e
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);
}
And sometimes I get the exception "The filename, directory name, or volume label syntax is incorrect" if useShellExecute is set to false
Any Ideas as to why this is not coming out correctly
You can't put an entire commandline in the FileName
property.
Instead, you should just Start
the txt file, which will open in the user's default editor:
Process.Start(@"C:\Users\Karthick\AppData\Local\Temp\5aau1orm.txt");
As mentioned by @SLaks, this is the appropriate way let the default application (in your case mapped to the .txt extension) open the file
Process.Start("test.txt");
But if you prefer to open text files only in Notepad and not in other default Text Editor
ProcessStartInfo processStartInfo = new ProcessStartInfo(@"c:\Windows\System32\notepad.exe", "text.txt");
Process.Start(processStartInfo);
You are trying to execute c:\\windows\\system32\\notepad.exe C:\\Users\\Karthick\\AppData\\Local\\Temp\\5aau1orm.txt
. If you aren't using a shell, it will be interpreted literally. If you use the shell, the shell will take care of the argument parsing.
Use the ProcessStartInfo.Arguements
property to supply arguments instead.
FileName property dosen't take command line type syntax. What you have specified is for command line.
Since its only a .txt
file you can use Process.Start() Method with full file path. It will automatically search for the respective default program to open the file.
精彩评论