• C#执行命令行 和 bat文件


    1.命令行直接执行:

    private bool runCmd(string cmd)
            {
                var output = string.Empty;
                var p = new Process();
                p.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.OutputDataReceived += (sender, args) => { output += args.Data; };
                p.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.Is64BitProcess ? Environment.SpecialFolder.System : Environment.SpecialFolder.SystemX86), "cmd.exe");
                p.StartInfo.Arguments = @"/C " + cmd;
                p.StartInfo.Arguments += "&&echo 1";
                p.StartInfo.Verb = "runas";
                p.Start();
                p.StandardInput.AutoFlush = true;
                p.BeginErrorReadLine();
                p.BeginOutputReadLine();
                p.WaitForExit();
    
                var resultCode = 0;
                if (int.TryParse(output, out resultCode))
                {
                    if (resultCode == 1)
                        return true;
                }
    
                return false;
            }

    2. 执行 bat 文件

    bat 文件:

    EXIT 10
    
    timeout /t 100

    C#

    var path = Application.StartupPath + @"	est.bat";
                var p = new Process();
                p.StartInfo.WorkingDirectory = Application.StartupPath;
                p.StartInfo.FileName = path;
                p.Start();
                p.WaitForExit();
                var a = p.ExitCode;
                    
  • 相关阅读:
    php求2个文件相对路径
    [JZOJ 5818] 做运动
    [JZOJ 5819] 大逃杀
    [JZOJ 5852] 相交
    [JZOJ 5817] 抄代码
    [JZOJ 5791] 阶乘
    [转载](asp.net大型项目实践)
    [转载](你必须知道的.net)
    [转载](闲话WPF)
    .net之 HtmlInputFile
  • 原文地址:https://www.cnblogs.com/nanfei/p/13929943.html
Copyright © 2020-2023  润新知