C# Start()使用问题记录

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);
            }

posted on 2016-04-10 15:38  johnphan  阅读(362)  评论(0编辑  收藏  举报

导航