c# Process cmd 开机自启动 时异常退出 不执行 问题

这几天,做了一个开机自己启动的程序,程序中调用到dos执行的exe,该程序手动打开是执行正常,开机自启动,dos调用的命令老不执行,无法通过命令执行程序。跟踪log,排查问题,最后定位到这里,加入这行代码

process.StartInfo.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory;

程序方法是这样写的

        public static string ExecuteDosCmd(string dosCommand, int milliseconds)
        {
            //输出字符串
            string output = "";     
            if (dosCommand != null && dosCommand != "")
            {
                //创建进程对象
                Process process = new Process();    
                //指定执行路径,开机自己启动时不指定路径,cmd异常退出
                process.StartInfo.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
                //设定需要执行的命令
                process.StartInfo.FileName = "cmd.exe";
                //不使用系统外壳程序启动
                process.StartInfo.UseShellExecute = false;
                //不重定向输入
                process.StartInfo.RedirectStandardInput = true;
                //重定向输出,而不是默认的显示在dos控制台
                process.StartInfo.RedirectStandardOutput = true;
                //输出错误信息
                process.StartInfo.RedirectStandardError = true;
                //不创建窗口
                process.StartInfo.CreateNoWindow = true;
                //设定参数,其中的“/C”表示执行完命令后马上退出
                process.StartInfo.Arguments = "/C  " + dosCommand;   
                try
                {
                    //开始进程
                    if (process.Start())       
                    {
                        if (milliseconds == 0)
                        {
                            //这里无限等待进程结束
                            process.WaitForExit();     
                        }
                        else
                        {
                            //这里等待进程结束,等待时间为指定的毫秒
                            process.WaitForExit(milliseconds);  
                        }
                        //读取进程的输出结果。
                        output = process.StandardOutput.ReadToEnd();
                        if (process.ExitCode == 0)
                        {
                            return "export success";
                        }
                        else
                        {
                            //WriteLogToFile("export 命令执行失败 process.ExitCode= "+ process.ExitCode);
                            return "export  Error";
                        }
                    }
                }
                catch (Exception ex)
                {
                    //WriteLogToFile("export 命令执行失败  错误消息:" + ex.Message);
                    output = ex.Message;
                }
                finally
                {
                    if (process != null)
                    {
                        process.Close();
                        process.Dispose();
                    }
                }
            }
            return output;
        }
        #endregion

加入以下这行代码 开机自启动没有问题了,不加的话,开机不执行dos 命令 ,异常退出
//指定执行路径,开机自己启动时不指定路径,cmd异常退出
process.StartInfo.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory;

猜你喜欢

转载自blog.csdn.net/guoruijun_2012_4/article/details/85318897
今日推荐