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

时间:2015-09-22 10:29:27

标签: c# winforms svn cmd

我正在尝试在我的程序后台的cmd中运行这些命令:

  • 转到该目录并输入'svn log --xml -v> svn.log
  • 更改回c:\ statsvn目录
  • 输入'java -jar statsvn.jar c:\ myproject \ svn.log c:\ myproject'

我不确定应该从哪里开始,但尝试过这个:

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...
        }

    }

我的代码的总体目标是根据已提交给SVN的更改生成xml文件,需要运行的命令行应该创建一个xml文件。我想在程序的后台执行此操作,而不显示cmd。

任何帮助都会很棒。

谢谢

1 个答案:

答案 0 :(得分:0)

您正在尝试运行位于startInfo.FileName路径的文件中的程序,而不是您的脚本。将要执行的脚本保存到文件中,将脚本文件的路径传递给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...
}