使用 C# 控制台应用程序的 Cmd,这些命令有效吗?

Cmd with c# console app, does these commands work?

嗯...一切都在标题中,这是一个 C# 控制台应用程序,我正在尝试执行这些命令...它似乎不起作用,但为什么呢?

它只是打开一个命令,我没有看到任何类似命令的东西

static void Main(string[] args)
    {
        string strCmdText = "mysqlcheck -r JAMFSOFTWARE -u root -p password";
        System.Diagnostics.Process.Start("CMD.exe", strCmdText);

        string strCmdText2 = "mysqlcheck -o JAMFSOFTWARE -u root -p password";
        System.Diagnostics.Process.Start("CMD.exe", strCmdText);

        string strCmdText3 = "mysqlcheck -c JAMFSOFTWARE -u root -p password";
        System.Diagnostics.Process.Start("CMD.exe", strCmdText);

    }

编辑:问题是我不想将该密码放在一个可以轻松访问的批处理文件中,所以基本上任何解决方案都可以,只要没有人能看到命令文件中的内容

接近解决方案:

    static void Main(string[] args)
    {

        ProcessStartInfo startInfo = new ProcessStartInfo("mysqlcheck");
        startInfo.Arguments = "-c JAMFSOFTWARE -u root -p !!fakepassword!!";


        Process process = new Process();
        process.StartInfo = startInfo;

        process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);

        try
        {
            process.Start();

        }
        catch
        {
        }

    }
    static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        StreamWriter sw = new StreamWriter("C:\JAMFCHECK.txt");
        sw.WriteLine( e.Data);
    }

现在我不知道为什么,但它要求输入密码...

您需要使用 /C:

启动 strCmdText
static void Main(string[] args)
{
    string strCmdText = "/C mysqlcheck -r JAMFSOFTWARE -u root -p password";
    System.Diagnostics.Process.Start("CMD.exe", strCmdText);

    string strCmdText2 = "/C mysqlcheck -o JAMFSOFTWARE -u root -p password";
    System.Diagnostics.Process.Start("CMD.exe", strCmdText);

    string strCmdText3 = "/C mysqlcheck -c JAMFSOFTWARE -u root -p password";
    System.Diagnostics.Process.Start("CMD.exe", strCmdText);

}

根据您的评论,我认为您不需要 CMD 您可以使用不同的可执行文件直接启动 'mysqlcheck'(见下文)

从命令行获取所有结果可能有点棘手。 我在答案末尾添加了一个工作示例,但我想稍微解释一下。

首先设置要执行的内容

        FileInfo executable = new FileInfo(@"C:\Temp\cmd.bat");

        ProcessStartInfo startInfo = new ProcessStartInfo(executable.FullName);
        startInfo.Arguments = "two arguments";
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardOutput = true;

确保重定向输​​出(我也不想看到它) 现在您将 StartInfo 提供给进程

        Process process = new Process();
        process.StartInfo = startInfo;

因为我们想要读取我们需要的输出

        process.EnableRaisingEvents = true;

我分配了两个事件:

        process.OutputDataReceived += new ...
        process.Exited += new ...

启动进程并开始阅读

        process.Start();
        process.BeginOutputReadLine();

我使用的批处理文件给了我文件和参数

@echo off
echo %~nx0
echo %1
echo %2

你可以像这样调用任何可执行文件(除了 cmd.exe,它永远不会完成)

我附上了完整的代码,还添加了一个重置​​事件来同步执行。这是我的调试控制台的输出:

  • 开始
  • 正在启动外部应用程序:C:\Temp\cmd.bat
  • cmd.bat
  • 两个
  • 参数
  • 完成执行外部应用程序
  • 结束

列表:

    // using System.Diagnostics;
    // using System.Threading;
    // using System.IO

    private ManualResetEvent mre;

    private void button_Click(object sender, EventArgs e)
    {
        mre = new ManualResetEvent(false);
        Debug.WriteLine("Beginn");

        FileInfo executable = new FileInfo(@"C:\Temp\cmd.bat");

        ProcessStartInfo startInfo = new ProcessStartInfo(executable.FullName);
        startInfo.Arguments = "two arguments";
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardOutput = true;

        Process process = new Process();
        process.EnableRaisingEvents = true;
        process.StartInfo = startInfo;
        process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
        process.Exited += new EventHandler(process_Exited);

        Debug.WriteLine(String.Format("Starting external Application: {0}", executable.FullName));
        process.Start();
        process.BeginOutputReadLine();

        mre.WaitOne();
        Debug.WriteLine("End");
    }

    void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        Debug.WriteLine(e.Data);
    }

    void process_Exited(object sender, EventArgs e)
    {
        Debug.WriteLine("Finished executing external Application");
        mre.Set();
    }

希望对您有所帮助,此致