简体   繁体   English

在Windows窗体应用程序的后台在cmd中运行命令

[英]running a command in the cmd in the background of a windows form application

I'm trying to run these commands in the cmd in the background of my program: 我正在尝试在程序后台在cmd中运行以下命令:

  • Change into that directory and type 'svn log --xml -v > svn.log 切换到该目录并输入'svn log --xml -v> svn.log
  • Change back to the c:\\statsvn directory 切换回c:\\ statsvn目录
  • type 'java -jar statsvn.jar c:\\myproject\\svn.log c:\\myproject' 键入'java -jar statsvn.jar c:\\ myproject \\ svn.log c:\\ myproject'

I'm unsure where I should start but have tried this: 我不确定应该从哪里开始,但是已经尝试过了:

private void FileSelect_Click(object sender, EventArgs e)
    {
        string databaseDirectory = saveAndLoad.getFolderContentsFilename();
        FilePath.Text = databaseDirectory;
    }

    private void xmlGenerator_Click(object sender, EventArgs e)
    {
        string file = FilePath.ToString();
        var process = new Process();
        var startInfo = new ProcessStartInfo
        {
            WindowStyle = ProcessWindowStyle.Hidden,
            FileName = "path to your script file..."
        };

        process.StartInfo = startInfo;

        process.Start();
        process.WaitForExit();

        if (process.ExitCode == 0)
        {
            //success
        }
        else
        {
            //an error occured during script execution...
        }

    }

The overall aim of my code is to generate an xml file based on changes that have been committed to SVN the command lines that need to run should create an xml file. 我的代码的总体目标是基于已提交给SVN的更改来生成xml文件,需要运行的命令行应创建一个xml文件。 I'd like to do this in the background of the program without the cmd being displayed. 我想在程序的后台执行此操作,而不会显示cmd。

Any help would be great. 任何帮助都会很棒。

Thanks 谢谢

You are trying to run program located in in a file at startInfo.FileName path, not your script. 您正在尝试运行位于startInfo.FileName路径的文件中的程序,而不是脚本。 Save your script that you want to execute to a file, pass the path to the script file to your StartInfo object, start the process and wait for its execution: 将要执行的脚本保存到文件中,将脚本文件的路径传递给StartInfo对象,启动进程并等待其执行:

var process = new Process();
var startInfo = new ProcessStartInfo
{
    WindowStyle = ProcessWindowStyle.Hidden,
    FileName = "path to your script file..."
};

process.StartInfo = startInfo;

process.Start();
process.WaitForExit();

if (process.ExitCode == 0)
{
    //success
}
else
{
    //an error occured during script execution...
}

I use the following function for any command, it always works: here you would need to pass two arguments. 我对任何命令使用以下函数,该函数始终有效:在这里,您需要传递两个参数。 1) your command and 2) your directory. 1)您的命令和2)您的目录。

 private string BatchCommand(string cmd, string mapD)
    {
        Batchresults = "";

        System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + cmd);
        procStartInfo.WorkingDirectory = mapD;
        // The following commands are needed to redirect the standard output.
        // This means that it will be redirected to the Process.StandardOutput StreamReader.
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.RedirectStandardError = true;
        procStartInfo.RedirectStandardInput = true;
        procStartInfo.UseShellExecute = false;
        // Do not create the black window.
        procStartInfo.CreateNoWindow = true;
        // Now we create a process, assign its ProcessStartInfo and start it
        System.Diagnostics.Process cmdProcess = new System.Diagnostics.Process();
        cmdProcess.StartInfo = procStartInfo;
        cmdProcess.ErrorDataReceived += cmd_Error;
        cmdProcess.OutputDataReceived += cmd_DataReceived;
        cmdProcess.EnableRaisingEvents = true;
        cmdProcess.Start();
        cmdProcess.BeginOutputReadLine();
        cmdProcess.BeginErrorReadLine();
        cmdProcess.StandardInput.WriteLine("exit");                  //Execute exit.
        cmdProcess.WaitForExit();

        // Get the output into a string

        return Batchresults;
    }
    static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
    {
        if (e.Data != null)
            Batchresults += Environment.NewLine + e.Data.ToString();

    }

     void cmd_Error(object sender, DataReceivedEventArgs e)
    {
        if (e.Data != null)
        {
            Batchresults += Environment.NewLine + e.Data.ToString();

        }
    }

note that Batchresults is a global string. 请注意,Batchresults是全局字符串。

call the script as 将该脚本称为

string result= BatchCommand("Your command here","Directory from where you want to execute it");

string result will contains details of all result and errors as well 字符串结果还将包含所有结果和错误的详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM