Example #1
0
        /// <summary>
        /// 运行CMD并读取结果(建议执行返回数据较小的命令)
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public static List <string> Execute(string cmd, WindowsAccountModel account = null)
        {
            List <string> result  = null;
            StreamReader  reader  = null;
            Process       process = null;

            try
            {
                process = GetProcess(account: account);
                process.Start();
                process.StandardInput.WriteLine(cmd);
                process.StandardInput.WriteLine("exit");
                reader = process.StandardOutput;
                result = new List <string>();
                do
                {
                    string line = reader.ReadLine();
                    if (Str.Ok(line))
                    {
                        result.Add(line.Trim());
                    }
                } while (!reader.EndOfStream);
                process.WaitForExit();
            }
            catch { }
            finally
            {
                reader?.Close();
                process?.Close();
                process?.Dispose();
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// 开始运行CMD命令
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="output">输出动作</param>
        public static void Execute(string cmd, Action <string> output, WindowsAccountModel account = null)
        {
            Process      process   = null;
            StreamReader outReader = null;
            StreamReader errReader = null;

            try
            {
                process = GetProcess(account: account);
                process.Start();
                process.StandardInput.AutoFlush = true;
                process.StandardInput.WriteLine(cmd);
                process.StandardInput.WriteLine("exit");

                outReader = process.StandardOutput;
                errReader = process.StandardError;

                ReaderAction(outReader, output);
                ReaderAction(errReader, output);

                process.WaitForExit();
                process.Close();
            }
            catch { }
        }
Example #3
0
        /// <summary>
        /// 创建cmd的进程
        /// </summary>
        /// <returns></returns>
        public static Process GetProcess(string verb = "RunAs", WindowsAccountModel account = null)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();

            if (account != null && account.Check())
            {
                if (Str.Ok(account.Domain))
                {
                    startInfo.Domain = account.Domain;
                }
                if (Str.Ok(account.UserName))
                {
                    startInfo.UserName = account.UserName;
                }
                if (Str.Ok(account.Password))
                {
                    startInfo.Password = ProcessStarter.ConvertToSecureString(account.Password);
                }
            }

            startInfo.FileName               = "cmd.exe";
            startInfo.Arguments              = @"/c C:\Windows\System32\cmd.exe";
            startInfo.RedirectStandardInput  = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError  = true;
            startInfo.UseShellExecute        = false;
            startInfo.CreateNoWindow         = true;
            startInfo.Verb = verb;
            Process process = new Process();

            process.StartInfo = startInfo;
            return(process);
        }
 /// <summary>
 /// 卸载服务(关闭、删除,一条龙服务)
 /// </summary>
 /// <param name="name"></param>
 /// <param name="account">运行账户信息</param>
 public static bool Uninstall(string name, WindowsAccountModel account = null)
 {
     try
     {
         List <string> stopResult   = CMDProcessTool.Execute($"sc stop {name}", account);
         List <string> deleteResult = CMDProcessTool.Execute($"sc delete {name}", account);
         List <string> queryResult  = CMDProcessTool.Execute($"sc query {name}");
         if (Ls.Ok(queryResult))
         {
             return(queryResult.Any(x => x.Contains("失败") || x.Contains("1060") || x.Contains("服务未安装")));
         }
         return(false);
     }
     catch { return(false); }
 }
 /// <summary>
 /// 安装服务(安装、运行、设置自启,一条龙服务)
 /// </summary>
 /// <param name="name">服务名</param>
 /// <param name="path">应用程序路径</param>
 /// <param name="account">运行账户信息</param>
 /// <returns></returns>
 public static bool Install(string name, string path, WindowsAccountModel account = null)
 {
     try
     {
         List <string> createResult = CMDProcessTool.Execute($"sc create {name} binPath= \"{path}\"", account);
         List <string> startResult  = CMDProcessTool.Execute($"net start {name}", account);
         List <string> configResult = CMDProcessTool.Execute($"sc config {name} start= AUTO", account);
         if (Ls.Ok(createResult))
         {
             return(createResult.Any(x => x.Contains("成功") || x.Contains("服务已存在")));
         }
         return(false);
     }
     catch { return(false); }
 }
Example #6
0
        /// <summary>
        /// 开始运行CMD命令
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="output">输出动作</param>
        public static void Execute(string cmd, Action <string> output, WindowsAccountModel account = null)
        {
            StreamReader reader  = null;
            Process      process = null;

            try
            {
                process = GetProcess(account: account);
                process.Start();
                process.StandardInput.AutoFlush = true;
                process.StandardInput.WriteLine(cmd);
                process.StandardInput.WriteLine("exit");
                reader = process.StandardOutput;
                do
                {
                    string line = reader.ReadLine();
                    output?.Invoke(line);
                } while (!reader.EndOfStream);
                process.WaitForExit();
                process.Close();
            }
            catch { }
        }