Making directories with command line on C# - 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.

Related

Process Arguments not being run

I'm trying to start a local instance of notepad with a text file to try out c# cmd line arguments for eventual use in a remote connection script. I'm using System.Diagnostics.Process, but the StartInfo.Arguments doesn't actually run completely and open the notepad instance.
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = #"cd\ start notepad C:\test\testcmdline.txt";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
The window opens at root, which tells me the cd\ is working, but the "start notepad" does not seem to be running.
Am I missing something about the structure of StartInfo.Arguments?
EDIT: I'm trying to figure out how to run a python script on a remote server, and using this as a test for running things in cmd in c#. While it's fine to run this in notepad, I'm not sure if the principle would carry over to the eventual implementation of running a python script remotely so I'm attempting to learn how to run items through cmd in C# in general.
I ended up using the more simple 2 arg Process.Start.
string cmdText;
cmdText = #"/C C:\test\testcmdline.txt";
Process.Start("cmd.exe", cmdText);
Try adding /c in the beginning of the Arguments.
Or the above task can be done as below
var process = new ProcessStartInfo
{
FileName = "cmd.exe",
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = #"/c start notepad C:\test\testcmdline.txt"
};
Process.Start(process );

How to pass parameters to an exe?

I am using psexec on my server to run an exe file on another server. How do I pass parameters to the other exe ?
The exe that I am running on my server is psexec which in turn must run the exe named vmtoolsd.exe located on another system. How do I pass parameters to vmtoolsd.exe ? Also, where do I pass it ? Would I pass it as part of the info.Arguments ? I've tried that but it isn't working.
ProcessStartInfo info = new ProcessStartInfo(#"C:\Tools");
info.FileName = #"C:\Tools\psexec.exe";
info.Arguments = #"\\" + serverIP + #"C:\Program Files\VMware\VMwareTools\vmtoolsd.exe";
Process.Start(info);
Also, as part of info.Arguments would I have to preface the path of vmtoolsd.exe with the IP address, followed by the drive path ?
Hope the below code may help.
Code from first .exe:
Process p= new Process();
p.StartInfo.FileName = "demo.exe";
p.StartInfo.Arguments = "param1 param2";
p.Start();
p.WaitForExit();
or
Process.Start("demo.exe", "param1 param2");
Code in demo.exe:
static void Main (string [] args)
{
Console.WriteLine(args[0]);
Console.WriteLine(args[1]);
}
Right click on .exe file-->goto shortcut-->in target tab write the arguement in extreme right...
in my case it worked
You can see it in the following post (answer by #AndyMcCluggage):
How do I start a process from C#?
using System.Diagnostics;
...
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = "-n";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
process.WaitForExit();// Waits here for the process to exit.
It provides far more control as you can see in the MSDN, but basically the arguments control is quite easy as you can see, is just to modify a property with a string.
Update: Since with the snippet code above you would be starting PsExec, based on:
PsExec
The format you have to use is:
psexec #run_file [options] command [arguments]
Where: arguments Arguments to pass (file paths must be absolute paths on the target system).
Since the process you're starting is psexec, in the process.StartInfo.Arguments you would have to put all the parameters it would need, in a sigle chain: #run_file [options] command [arguments].
Step1.Create Shortcut and then Right click on Shortcut-->click properties page then target tab write the comment line argument in extreme right... this way worked for me

Unable to execute cmd commands from 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();

how to hide windows host script c#.net

I need hide hide Windows host script after this code:
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 slmgr.vbs /dlv";
process.StartInfo = startInfo;
process.Start();
Does anyone know how I could do this?
If you are talking about just hiding the window completely then you are missing setting the CreateNoWindow property.
startInfo.CreateNoWindow = true;
Instead of firing up an additional command-prompt, use cscript.exe to execute your VB script. If you do it this way you won't have to worry about the shell figuring out how to execute you vbs file and you won't get an additional command-line window.
startInfo.FileName = "cscript.exe";
startInfo.Arguments = "slmgr.vbs /dlv";
If the executable that is creating this process is not in the same directory as slmgr.vbs, you'll also need to set the full path to the file in the arguments, or set the working directory that the process runs in.
// Example path where your scripts could reside.
startInfo.WorkingDirectory = #"C:\PathToMyScripts\VBScripts\";
You probably want to redirect the output as well so you can log it somewhere.
Thank You for mr.BrutalDev
the second step
change startInfo.Arguments = "slmgr.vbs /dlv";
to startInfoent.Arguments = "C:\\Windows\\System32\\slmgr.vbs /dlv";

Commandline Arguments in C#

Hello again Stackoverflow community,
Today I am trying to execute an application with commandline parameters in C#, that not realy difficult like I tried
Process.Start(foldervar + "cocacola.exe", "pepsi.txt");
Cocacola.exe writes and Log in its current folder. In my commandline I write it manually like this
C:\myfolder>cocacola.exe pepsi.txt
Works wonderful but if I try it in C# a total fail.
I read that C# parses the command as C:\myfolder>cocacola pepsi.txt, without the ".EXE" ending. And I tested it manually without the ending, and this does not work.
Now, my question is what is the correct way to get C# executing it C:\myfolder>cocacola.exe pepsi.txt with the ".EXE"
use ProcessStartInfo
http://www.dotnetperls.com/process-start
example:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.WorkingDirectory=#"c:\someplace";
proc.StartInfo.FileName="cocacola.exe";
proc.StartInfo.Arguments="pepsi.txt";
proc.Start();
proc.WaitForExit();
here is docs on the StartInfo properties:
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx
Try setting the StartInfo properties.
Process process = new Process();
process.StartInfo.FileName = #"C:\myfolder\cocacola.exe";
process.StartInfo.Arguments = #"C:\myfolder\pepsi.txt";
process.Start();
ProcessStartInfo has the WorkingDirectory property you should set to C:\myfolder
see: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx
You need to set the working directory first
string foldervar = #"C:\myfolder";
Process process = new Process();
process.StartInfo.WorkingDirectory = foldervar;
process.StartInfo.FileName = #"cocacola.exe";
process.StartInfo.Arguments = #"pepsi.txt";
process.Start();
Setting the WorkingDirectory is equivilent to cding into the proper directory before running programs. It's what relative paths are relative to.

Categories

Resources