Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi

I need to run the content of the batch file from C#. How to do that?

Content of the batch file:

echo off
\\server-name\directory\PsExec.exe -u domain\user-id -p ****** -d -e cmd.exe /c \\server-name\directory\file.exe


I require this to run a software with admin id in system where user does'nt have admin access.

Please post the complete code with all the namespace details since I'm a beginner.

Note: i dont want to have a batch file, i want to write everything in C#.
Posted
Updated 31-Aug-17 17:28pm
v2

You can use one of the methods System.Diagnostics.Process.Start, for example:

C#
System.Diagnostics.Process.Start(myBatchFileName);
or
C#
System.Diagnostics.Process.Start(myBatchFileName, myBatchParameters);


All parameters are strings. First parameter is the batch file name (don't use CMD.EXE or something, use the batch file name only) and the optional second parameter is the list of batch file arguments written in one string, separated with black spaces.

You can also assign the reference to the child process you start as it is returned by these methods. You can use this variable for different purposes. In particular, you can put your calling thread of your parent process to a wait state until the batch file completes its execution using Process.Wait.

Finally, you can hide the console, redirect all the output of your batch file to a stream and read all text output from this stream. You will need to redirect at least to streams: StandardOutput and StandardError.

Please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

You can find re-direction example here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

—SA
 
Share this answer
 
Comments
koolprasad2003 23-Jan-12 1:13am    
gooa answer SA +5
Sergey Alexandrovich Kryukov 23-Jan-12 1:23am    
Thank you, Prasad.
--SA
Use following code, which will write and run .bat file.
C#
System.IO.StreamWriter SW = new System.IO.StreamWriter("test.bat");
SW.WriteLine(@"\\server-name\directory\PsExec.exe -u domain\user-id -p ****** -d -e cmd.exe /c \\server-name\directory\file.exe");
SW.Flush();
SW.Close();
SW.Dispose();
SW = null;
System.Diagnostics.Process.Start("test.bat");


[edit]Added @ sign in front of string to prevent errors caused by unescaped backslash characters.[/edit]
 
Share this answer
 
v3
Comments
Member 8534726 23-Jan-12 1:50am    
Thanks Prasad for the solution.

I'm getting the following error.

1. the type or namespace name IOSW cannot be found.
2. Unrecognized escape sequence in the path ("\\server-name\directory\PsExec.exe -u domain\user-id -p ****** -d -e cmd.exe /c \\server-name\directory\file.exe")
Richard MacCutchan 23-Jan-12 12:39pm    
See my edit above.
Member 8534726 24-Jan-12 1:32am    
thanks Richard !

now I'm getting the below errors

1. The type or namespace name 'IOSW' does not exist in the namespace 'System' (are you missing an assembly reference?)

2. The type or namespace name 'System' does not exist in the namespace 'System' (are you missing an assembly reference?)

Kindly guide me
As SA told, use System.Diagnostics.Process namesapce to runout your batch file.
you need to provide filepath to Process.Start method
C#
Process.Start(//BatchFileFullPath);
 
Share this answer
 
v2
Comments
Member 8534726 23-Jan-12 1:23am    
Hi,

I dont like to have a batch file, i want to write my batch file commands in Code (C#).

Kindly help
[no name] 31-Oct-13 12:37pm    
very good
Sergey Alexandrovich Kryukov 23-Jan-12 1:25am    
You should have told us so. And why? Do you want to write them or run. If you want to write them, this is nothing more that writing some text (string manipulations). Do you really have a problem with that?
--SA
lukeer 3-May-12 3:04am    
Just adapt what SA wrote. The Process.Start() isn't tied to batch files. It can execute every executable.

System.Diagnostics.Process.Start(@"\\server-name\directory\PsExec.exe", @"-u domain\user-id -p ****** -d -e cmd.exe /c \\server-name\directory\file.exe");
Sergey Alexandrovich Kryukov 23-Jan-12 1:27am    
Prasad, please excuse me. If you reference my answer, what your own answer adds to it? (So, I did not vote). And your quotation marks are probably put by mistake. You certainly mean a string variable containing actual path to the file.
--SA
you can use as
C#
using System.IO;
using System.Diagnostics;
.
.
.

StreamWriter sw = new StreamWriter("a.bat");

sw.WriteLine("del *.dll");
sw.Close();

Process.Start("a.bat");
 
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