Unable to execute cmd commands from C# - c#

I want to execute cmd commands from Visual Studio (using c#). There are two commands which I want to run.
I referred this post, but not working in my case.
My code is:
private void ExecuteCmdCommands()
{
string strCmdText;
strCmdText = #"cd C:\Test + makecab /f wsp.ddf";
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = strCmdText;
process.StartInfo = startInfo;
process.Start();
}
When I run this code, only command prompt is open no commands are executed. What am I missing?

Don change directory, simply shell open the file:
strCmdText = #"C:\Test + makecab /f wsp.ddf";
Edit: Set the working directory:
startInfo.WorkingDirectory = #"C:\Test";

In order to call 2 command from one line, you need to use the & sign.
In your case:
#"/c cd C:\Test & makecab /f wsp.ddf";
Also dont forget the /c flag, telling the cmd to execute the command.

try to change to strCmdText = #"C:\Test + makecab /f wsp.ddf";

Just a guess, but it looks like you are attempting to execute 2 different command on the same line??
First changing the directory is not necessary, and you don't need to execute cmd.exe. Just create a process directly for the makecab program.
startInfo.Filename = #"makecab.exe";
startInfo.Argumanets = #"/f c:\test\wsp.ddf";

The issue here is that the commands you're passing in are arguments and not commands (which would have to be piped in through the StandardInput pipe). Fortunately, you can use the "/c" argument as mentioned here. I'm not sure if it will work with the "+" operator.
Note, as someone else mentioned, you should also set the working directory using the available property or your program will fail if not run with a "C:" working directory.

you have to do run shell commands from C#
string strCmdText;
strCmdText= "/C copy /b Image1.jpg + xyz.rar Image2.jpg"; System.Diagnostics.Process.Start("CMD.exe",strCmdText);
**EDIT:**
This is to hide the cmd window.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C copy /b Image1.jpg + xyz.rar Image2.jpg";
process.StartInfo = startInfo;
process.Start();

Related

Invoking cmd command in c# (Visual Studio)

I was trying to invoke cmd command in c# and I have found this code:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "msg %username% Hello World";
process.StartInfo = startInfo;
process.Start();
This should popup a dialog box in the screen, with text "Hello World". (Code hides the cmd window). But, nothing happens. I also used debugging methods, to be 100% sure that program reaches this code. It does, but it does not display the popup message. Any ideas where I may be mistaking?
You need to change the Arguments to this:
startInfo.Arguments = "/C msg %username% Hello World";
cmd.exe needs the /C option. This tells cmd.exe to execute the following command and terminate again.
You can use cmd /? in a console window to show all options and parameters of cmd.exe.

Making directories with command line on C#

System.Diagnostics.Process process = new System.Diagnostics.Process ();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo ();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "md " + Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
process.StartInfo = startInfo;
process.Start ();
I'm attempting to make a directory on the desktop with this command, it doesn't make one however. Can anyone tell me why?
Just do this:
Directory.CreateDirectory(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
"my new folder name"));
Always prefer using the .NET class library instead of invoking external processes to do your work, unless you have a very specific reason not to do so.
One of the reasons your code is not working is because you are using the wrong syntax for cmd.exe. In order to pass a command as an argument, you have to use the following with the /K switch (use cmd /? for more information):
cmd.exe /K MD "c:\test\blah"
Another reason your code won't work is that the path you're providing to the MD command is just the path to the desktop itself:
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
You have forgotten to append the name of the folder you want to create on the desktop.

Run multiple net use commands for a process using c#

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C net use W: \\\\abcdpc25\\views\\Projects ";
process.StartInfo = startInfo;
process.Start();
I want to run multiple net use commands and i am not sure how to go about it
I am not sure if something like below would work .
startInfo.Arguments = "/C net use W: \\\\abcdpc25\\views\\Projects
net use H: \\\\abcdpc25\\views\\Project456 net use E:
\\\\abcdpc25\\views\\Project123";
Use & as the separator between commands and it should work.
You can also put all the net use statements in a batch file and put the batch file name on your command line.
You can also launch multiple processes, one for each net use.

Is there anyway I can get the desired exit value from PsExec in C#?

This is the command I am currently using for remote installation using psExec.
psExec \\MyRemoteComputer -s -c MyInstallationFile.exe /q
When I issue this command in Command Prompt Window, it works fine. If MyRemoteComputer cannot be reached, it returns error. After that, when I check the exit code with "echo %errorlevel%", it returns 53, which is correct exit code.
I coded with C# using Process like this..
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = ".....psExec.exe";
startInfo.WorkingDirectory = ".....";
startInfo.Arguments = #"\\MyRemoteComputer -s -c MyInstallationFile.exe /q";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process proc = Process.Start(startInfo);
....
proc.WaitForExit();
Console.WriteLine(proc.ExitCode);
It runs fine..but..when I checked the ExitCode(Obviously, when myRemoteComputer cannot be reached), it was 6, which is other than 53.
It is sort of making sense why it is happening, but is there any way I can retrieve the 53 value, NOT 6?
I tried a couple of things..
cmd /c psExec.exe ....
created a batch file, then output %errorlevel% as a file inside that batch file...then I used this batch file in my ProcessStartInfo....
Environment.GetEnvironmentVariable
None of them worked at all...I can't get my desired return value at all in the code...
There is really no way to retrieve the value I want??
Thanks in advance.

Windows defrag is not recognized as an internal or external command, operable program or batch file

I'm working on making a tech-toolkit program, and included in this 'toolkit' will be a button which runs a defrag on the local disk. Currently the batch file I've made for this is simple, it just runs a basic fragmentation analysis:
defrag C: /A
The C# code behind the button that triggers this is:
System.Diagnostics.ProcessStartInfo procInfo =
new System.Diagnostics.ProcessStartInfo();
procInfo.Verb = "runas";
procInfo.FileName = "(My Disk):\\PreDefrag.bat";
System.Diagnostics.Process.Start(procInfo);
This code does exactly what I want, it calls UAC then launches my batch file with Administative Privledges. Though once the batch file is ran, the output I recieve to the command console is:
C:\Windows\system32>defrag C: /A
'defrag' is not recognized as an internal or external command,
operable program or batch file.
What causes this Error and how do I fix it?
Check to make sure your defrag.exe file actually exists in C:\Windows\System32.
Try fully qualifying the 'defrag' command as:
C:\WINDOWS\system32\defrag.exe C: /A
Open up a cmd prompt and run this command: defrag.exe /? and post in the question what you get.
First of all: set yout project Platform Target property to Any CPU and untick the Prefer 32-bit option (99,9% this is the issue). Then... why starting a batch that invokes the command when you can just do this?
ProcessStartInfo info = new ProcessStartInfo();
info.Arguments = "/C defrag C: /A";
info.FileName = "cmd.exe";
info.UseShellExecute = false;
info.Verb = "runas";
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
Works like a charm on my machine. For multiple commands:
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
Process cmd = Process.Start(info);
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(command1);
sw.WriteLine(command2);
// ...

Categories

Resources