谁能帮我执行这段代码 [英] Can any one help me out in executing this code

查看:98
本文介绍了谁能帮我执行这段代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


有人可以帮我执行这段代码吗?

Hi,
Can any one help me out in executing this code?

string command = @" tf help > tfshistory.txt";
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo(@"cmd", @" C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" + command);

// 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.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 proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
MessageBox.Show(result);




谢谢,
Prakash




Thanks,
Prakash

推荐答案

如果将问题分解为几个步骤,您将具有

1)启动命令处理器
2)运行vcvarsall.bat
3)运行"tf帮助> tfshistory.txt"

因此,您实际上想运行两个命令而不是一个,而这将需要使用&"运算符.

If you break the problem into steps you have

1) Start the command processor
2) run vcvarsall.bat
3) run "tf help > tfshistory.txt"

So you actually want to run two commands not one and that will require the use of the "&" operator.

private static void RunCmds() {
  const String batch1 = @"SetVars.cmd";
  const String batch2 = @"ReadVars.cmd";
  const String argsFormat = "/c {0} & {1} > output.txt";

  ProcessStartInfo psi = new ProcessStartInfo();
  psi.FileName = Environment.GetEnvironmentVariable("COMSPEC");
  psi.Arguments = String.Format(argsFormat, batch1, batch2);
  psi.UseShellExecute = false;
  psi.CreateNoWindow = true;
  Console.WriteLine("File: {0}", psi.FileName);
  Console.WriteLine("Args: {0}", psi.Arguments);
  Process p = Process.Start(psi);
  p.WaitForExit();
  Console.WriteLine(File.ReadAllText("output.txt"));
}



我用于测试的批处理文件是



My batch files used for testing are

REM SetVars.cmd
SET ANIMAL=Chinchilla
SET CHARACTERISTIC=fluffy


@echo off
REM ReadVars.cmd
echo %ANIMAL%s are %CHARACTERISTIC%



控制台输出是



and the console output is

File: C:\WINDOWS\system32\cmd.exe
Args: /c SetVars.cmd & ReadVars.cmd > output.txt
Chinchillas are fluffy



哦,太好了!



Ooh nice!


为什么不使用所需的命令创建自己的.bat文件?此调用似乎太重载了参数.您启动命令处理器,该处理器应执行.bat文件(实际上用于设置环境变量并在内部调用其他.bat文件),该处理器将接收您的命令参数,这些参数应该是带有参数和输出重定向的另一个命令调用...你期望吗?
Why don''t you create your own .bat file with the commands required? This call seems too overloaded with parameters. You launch command processor which should execute .bat file (actually used to set environment variables and calling other .bat files internally) which shall receive your command arguments which are supposed to be another command call with parameter and output redirection... seriously, what do you expect?


这篇关于谁能帮我执行这段代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆