C#执行命令行(兼容32位64位)

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

 

C#执行命令行(兼容32位64位)

上一篇:XMind 2021思维导图 完美中文免费版(Win+Mac)


下一篇:C# 中的类型和变量