您的位置:首页 > 编程语言 > C#

C# Start()使用问题记录

2016-04-10 15:37 204 查看
C# Start()使用问题记录
似乎已经完全按照要求,赋值了相关属性的数据,但是Start还是运行有问题。例如:提示 The directory name is invalid 等等。

需要注意:

运行的程序要给出全部的路径名 fullpathname。Process.StartInfo.FileName
如果下列参数为真,可能还需要给出工作目录名Process.StartInfo.WorkingDirectory:
Process.StartInfo.UseShellExecute = false; //false : no needProcess.StartInfo.WorkingDirectory

参数中不可以有重定向的字符,重定向需要设置StartInfo的其它属性才可以实现:
Process.StartInfo.Arguments = "... >> my.log"

如果使用 Process.StartInfo.FileName = "CMD"
Process.StartInfo.Arguments = """ + @"/C" + @"...." + """
参数部分可能需要应该作为整体的一个字符串,引号起来。这时,CMD启动的程序可以使用重定向,但不是CMD命令被重定向了。
有的程序只能识别8个有效的文件或目录名,需要ShortName转换一下。

如果Start的文件名和参数比较复杂,
最好使用,如下方法:
string tempbatfilename = Path.GetTempFileName() + ".bat"; //Creat a bat file
// get your CommandLineText & CommandArguments

using (StreamWriter swtmp = File.CreateText(tempbatfilename))

{

swtmp.WriteLine(CommandLineText + " " + CommandArguments);

}
......
try

{

myProcess.StartInfo.UseShellExecute = false;

myProcess.StartInfo.FileName = tempbatfilename;

myProcess.Start();

...

finally

{

File.Delete(tempbatfilename);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: