【问题标题】:How to run a command using command prompt in c# [duplicate]如何在c#中使用命令提示符运行命令[重复]
【发布时间】:2014-02-04 06:44:03
【问题描述】:

我正在使用 c# 开发一个小项目,我正在尝试在后台使用 windows 命令提示符运行命令(不显示命令提示符)。在使用之前,我已经在 vb.net 中完成了此操作: Shell("netsh wlan stop hostsnetwork", 0) ,但在 c# 中找不到如何操作。有人知道怎么做吗?

【问题讨论】:

    标签: c# cmd


    【解决方案1】:
       public static string ProcessStarter(string processName, string argString, string workingDirectory = null)
        {
            var prs = new Process();
            if (!string.IsNullOrWhiteSpace(workingDirectory))
            {
                prs.StartInfo.WorkingDirectory = workingDirectory;
            }
            prs.StartInfo.UseShellExecute = false;
            prs.StartInfo.RedirectStandardOutput = true;
            prs.StartInfo.RedirectStandardError = true;
            prs.StartInfo.FileName = processName;
            prs.StartInfo.Arguments = argString;
            // LOOK HERE
            prs.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
            prs.Start();
            string result = prs.StandardOutput.ReadToEnd();
            string resultErr = prs.StandardError.ReadToEnd();
            return string.IsNullOrEmpty(result) ? resultErr : result;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-01
      • 1970-01-01
      • 2021-08-28
      • 2019-05-28
      • 2010-11-30
      • 1970-01-01
      • 2011-10-26
      相关资源
      最近更新 更多