C# 在windows窗体应用程序后台的cmd中运行命令
我正试图在程序后台的cmd中运行以下命令:C# 在windows窗体应用程序后台的cmd中运行命令,c#,winforms,svn,cmd,C#,Winforms,Svn,Cmd,我正试图在程序后台的cmd中运行以下命令: 切换到该目录并键入“svnlog--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 =
- 切换到该目录并键入“svnlog--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
任何帮助都会很好
谢谢您正在尝试运行位于
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...
}
我对任何命令都使用以下函数,它总是有效的:这里需要传递两个参数。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();
}
}
请注意,Batchresults是一个全局字符串
将脚本称为
string result= BatchCommand("Your command here","Directory from where you want to execute it");
字符串结果将包含所有结果和错误的详细信息抱歉,我不理解“将要执行的脚本保存到文件中,将脚本文件的路径传递到StartInfo对象,启动进程并等待其执行”这一点,只需创建一个名为(例如)的简单文本文件
script.cmd
在解决方案中的某个地方,并将命令(即svnlog--xml-v>svn.log
)放入该文件中。文件存储在解决方案中的某个位置,因此您知道文件的相对路径-将相对路径传递给StartInfo
对象。您可以按上面的代码段所示运行脚本。运行脚本时,出现错误“Win32 Exception was unhandled”(Win32 Exception was unhandled),系统无法找到指定的文件。有什么建议吗?文件路径错误(如您的异常所示…)。文件的路径是什么?启动进程时,valuestartInfo.FileName
是什么?文件路径取决于用户选择的文件。由于该程序将用于检查多个文件,因此用户必须选择文件。我在Batchresults的何处定义了错误?您需要在类中定义它。在函数外部,在定义TextSaveAndLoad saveAndLoad=new TextSaveAndLoad()的地方定义它;我已经定义了它,但在cmd_DataReceived时出现了一个错误:“非静态字段、方法或属性'XML_generator.Form1.Batchresults'需要对象引用。您需要将其定义为静态字符串Batchresults;cmd_DataReceived函数是静态的。”。。