Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Can any one help me out in executing this code?
C#
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);



Thanks,
Prakash
Posted
Updated 15-Sep-11 2:52am
v3
Comments
CPallini 15-Sep-11 8:10am    
What problem(s) are you experiencing?
[no name] 15-Sep-11 8:21am    
What is the Problem
[no name] 15-Sep-11 8:55am    
Hi,

In my above code when i execute below command, the text data will be inserted into tfshistory.txt this is not working when i run the above code but works fine when i do it manually by going to VS 2008 command prompt & execute the beow command. So do help me in this regard
tf help > 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.

C#
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!
 
Share this answer
 
v2
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?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900