I want to write a command like pushd \172.x.x.x\something\something using c#. I just want to map the network path on the local machine using pushd. If I am doing the same by explicitly open the cmd n typing the pushd command , it is connecting the network path on the local machine. I want to do it by using c#.Any help would be appreciated. Below is my code :
private void button2_Click(object sender, EventArgs e)
{
string strCmdText;
strCmdText = "pushd" + #"\172.x.x.x\something\something";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
There's a space missing between 'pushd' and the second argument.
Change it to something like this:
strCmdText = "pushd" + " " + #"\172.x.x.x\something\something";
Does it help specifying /C or /K first in strCmdText?
Does it help using double backslash: \\172.x.x.x
Related
I am trying to execute one command through process but it is throwing exception as "The system cannot find the file specified". When i run this command directly on command prompt. It is working fine.
Command: start cmd.exe #cmd /k "NTttcpr.exe -r -m 1,*,192.168.1.2 -a 2 -t 120 -wu 10 -cd 10 >> NTTTCP-1T-TCP-IPV4-Rx-MTU1500-Support-port-1-Rx-AMD-10-GBE-RJ45-ITR-1.log"
This command executes perfectly if i run on command prompt.
This is how i written code:
string tool = #"NTttcpr.exe";
string command = " -r -m 1,*,192.168.1.2 -a 2 -t 120 -wu 10 -cd 10 >> NTTTCP-1T-TCP-IPV4-Rx-MTU1500-Support-port-1-Rx-AMD-10-GBE-RJ45-ITR-1.log";
private void RunCommand(string tool, string command)
{
try
{
logger.Info($"{MethodBase.GetCurrentMethod()}: {tool} {command}");
Process pro = new Process();
pro.StartInfo.FileName = "start cmd ";
pro.StartInfo.Arguments = "#cmd /k " + '"' + tool + " " + command + '"';
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardOutput = true;
logger.Info($"{MethodBase.GetCurrentMethod()}: Executing command: {tool} {command}");
pro.StartInfo.Verb = "runas";
pro.Start();
//pro.WaitForExit(MillisecondsTimeout);
//Thread.Sleep(MillisecondsTimeout);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
logger.Error($"{MethodBase.GetCurrentMethod()}: Exception occurred while uni-directional command!!");
logger.Error($"{MethodBase.GetCurrentMethod()}: {ex}");
}
}
Note:
NTttcpr.exe file is already present in current executing directory.
Please help me to solve this.
This should be because you have not set the working directory, add pro.StartInfo.WorkingDirectory = "path to NTttcpr.exe" do not add NTttcpr.exe, just add the location.
Let me know if this works.
cmd.exe is not required for Process class. Try like below.
pro.StartInfo.FileName = "NTttcpr.exe";
pro.StartInfo.Arguments = command
I have a windows form application where on a button click, a program is run that creates a file. I want to output the contents of that (txt) file into a textbox. I can see when debugging the file is created, but I get a File not found error. If I click continue, it then works.
private void button2_Click(object sender, EventArgs e)
{
string fp = EscapeArguments(filepath);
string strCmdText = "some command";
Process.Start("CMD.exe", strCmdText);
string dir = Path.GetDirectoryName(fp);
string name = Path.GetFileNameWithoutExtension(fp);
string txtfp = dir + "\\" + name + + ".txt";
string txtout;
if (File.Exists(txtfp))
{
txtout= File.ReadAllText(txtfp);
textBox1.Text = txtout;
}
}
It sounds like the process hasn't finished creating the file by the time your application is looking for it. Try waiting for the process to exit before continuing:
Process proc = Process.Start("CMD.exe", strCmdText);
proc.WaitForExit();
Im currently trying to run openVPN from a CMD that is programatically created.
Below is the code that I have created:
private void btnRunVpn_Click(object sender, EventArgs e)
{
string openVpnDir = #"""C:\Program Files\OpenVPN\bin\openvpn.exe""";
string myDir = #"""C:\Users\Jeremy\Desktop\OpenSource Rat\easyRDPClient\TutClient\bin\Debug\NewServerClient.ovpn""";
string configCommand = " --config ";
string command = openVpnDir + configCommand + myDir;
System.Diagnostics.Process.Start("CMD.exe", "/K " + command);
MessageBox.Show(command);
}
The message box displays the correct Command:
"C:\Program Files\OpenVPN\bin\openvpn.exe" --config "C:\Users\Jeremy\Desktop\OpenSource Rat\easyRDPClient\TutClient\bin\Debug\NewServerClient.ovpn"
However the command prompt is returning:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
The command is deffinatly correct because the message box that displays the command is correct, also when i copy what is in the message box into the command prompt it works perfectly.
wondering if it is anything to do with the two strings created that both have #"""message""" to display the message correctly. Wondering if that is the issue.
I can't seem escape the double quotes properly because it is a directory. So Im forced to use the #"""test""" to display a command like this "test"
Wondering if anyone understands this a little more than I do.
Would be great help.
Thankyou in advance!
I believe this will do what you are hoping to accomplish. It should leave the command line open, and correct the issue where the quotes are removed.
In the sample below, I added quotes around the command variable in the Start command.
private void btnRunVpn_Click(object sender, EventArgs e)
{
string openVpnDir = #"""C:\Program Files\OpenVPN\bin\openvpn.exe""";
string myDir = #"""C:\Users\Jeremy\Desktop\OpenSource Rat\easyRDPClient\TutClient\bin\Debug\NewServerClient.ovpn""";
string configCommand = " --config ";
string command = openVpnDir + configCommand + myDir;
System.Diagnostics.Process.Start("CMD.exe", "/K " + "\"" + command + "\"");
MessageBox.Show(command);
}
What I believe is happening is the CMD /K is expecting 1 argument. Since you are passing multiple, it is only looking at the first one (and removes the quotes when it does). This leaves it with:
C:\Program Files\OpenVPN\bin\openvpn.exe
It then cannot locate the application because of the space in the path.
By wrapping the entire command in quotes, it forces CMD to treat it as a single argument. It then removes the outer quotes and processes the desired command.
I tried it with Notepad in VB.NET, so hopefully I adjusted it correctly for your scenario.
problem in cmd.exe while i open it from c#
..
when i open start > run > cmd
and write this command ..
c:>msg \server:"192.168.6.5" * "send hello "
the obove command executed correctly
but
when i run cmd from c#.net this command get this error
private void button3_Click(object sender, EventArgs e)
{
Process.Start("cmd.exe");
}
and then write the same command
c:>msg \server:"192.168.6.5" * "send hello "
output message : msg is not recognized as an internal or external command...
what's the problem ??
plz help
1) If you're on 64 bit, you may have to use "C:\windows\sysnative\msg.exe". Or use System.Environment.GetEnvironmentVariable to get the path and then pass it in to a new ProcessStartInfo object. e.g.
string path = System.Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);
// add in the extra path parts if required
ProcessStartInfo pinfo = new ProcessStartInfo();
pinfo.WorkingDirectory = "whatever";
pinfo.FileName = "CMD.exe";
pinfo.Arguments = "whatever";
pinfo.EnvironmentVariables["Path"] = path;
Then pass that into Process.Start
2) Also, from the documentation "The user must have send message access permission to send a message.", so it will depend on what privileges you're running the process under.
I have sample code to run command but its not working ( just opens CMD ) without executing the command
string strCmdLine =
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " +
"--load-extension=\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\toolbar-GC\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
where is problem ?
You need to add a /C
Correct syntax for CMD.exe is
CMD.EXE /c command
string strCmdLine =
"/C C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " +
"--load-extension=\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\toolbar-GC\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
You don't need to use cmd.exe mate...
I guess this should do the job for you...
string strCmdLine =
"\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe \"";
var parmaters = "google.com";
System.Diagnostics.Process.Start(strCmdLine, parmaters);
you need the parameter "/c"
string strCmdLine =
"/c C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " +
"--load-extension=\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\toolbar-GC\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();