[英]How can I run a batch file from my Visual C# .NET code and retrieve the results?
I have created a getSvnUrl.bat 我创建了一个getSvnUrl.bat
containing the following code 包含以下代码
@ECHO OFF
for /f "tokens=1-2" %%i in ('"%VISUALSVN_SERVER%\bin\svn.exe" info C:\\Code\\ServiceRouter153\\SCM\\Configs\\ServiceRouter_MIGRATION.xml') do (
if "%%i"=="URL:" (
set URL=%%j
)
)
echo The result is.. %URL%
I would like to run this .bat from C# .NET code and be able to collect the SVN url contained the URL variable in a String variable in C#. 我想从C#.NET代码运行此.bat,并能够收集C#的String变量中包含URL变量的SVN url。
I tried this C# code from an 11 year old MSN blog http://blogs.msdn.com/b/csharpfaq/archive/2004/06/01/146375.aspx 我从11岁的MSN博客http://blogs.msdn.com/b/csharpfaq/archive/2004/06/01/146375.aspx尝试了此C#代码
It is not working. 它不起作用。 Is there a new way to do this?
有没有新的方法可以做到这一点?
private void runSyncAndGetResults_Click(object sender, System.EventArgs e)
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(@"C:\getSvnUrl.bat");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = listFiles.StandardOutput;
listFiles.WaitForExit(2000);
if (listFiles.HasExited)
{
string output = myOutput.ReadToEnd();
this.processResults.Text = output;
}
}
Here is the method I ended up using in case it ever helps someone else. 这是我最终使用的方法,以防其他人使用。
private string runBatchGetSvnUrl()
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(@"C:\getSvnUrl.bat");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = listFiles.StandardOutput;
listFiles.WaitForExit(2000);
if (listFiles.HasExited)
{
string output = myOutput.ReadToEnd();
return output;
}
else
{
return "It didn't work.";
//change this to throw an excetion later
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.