C# Window Process & CMD

    public class SysAccess
    {
        public static void ProcessStart(string processPath, params string[] contents)
        {
            if (string.IsNullOrEmpty(processPath))
                return;

            if (contents == null || contents.Length == 0)
                Process.Start(processPath);
            else
            {
                Process.Start(processPath, contents[0]); // 启动程序参数
                if (contents.Length > 1) // 启动程序后要键入的值
                {
                    System.Threading.Thread.Sleep(200);
                    for (int i = 1; i < contents.Length; i++)
                        System.Windows.Forms.SendKeys.SendWait(contents[i]);
                }
            }
        }

        public static void WinCmd(params string[] contents)
        {
            if (contents == null || contents.Length < 1)
                return;

            using (Process proc = new Process())
            {
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                foreach (var cmd in contents)
                {
                    proc.StandardInput.WriteLine(cmd);
                }
                proc.StandardInput.WriteLine("exit");
                proc.StandardInput.Flush();
            }
        }
    }

 

posted @ 2019-01-08 13:23  脑子不够用  阅读(887)  评论(0编辑  收藏  举报