my application will run a command that is like this:
wco -f "C:\Work\6.70 Ex\Master Build.Txt"
what i do is i usually open up cmd and type the above line manually.
i tried to automate this with a script:
string strCmdText = "wco -f C:\Work\6.70 ex\Master Build.Txt";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
however because of the white spaces it gets confusing.
please help
If you want to use cmd.exe to run a program, you need to add either the /C or /K switch. The /C switch runs the command and then exits cmd.exe. /K runs the command and then leaves cmd.exe open.
cmd.exe /K echo hello
I assume wco is a program of yours? If so, you can bypass using cmd.exe and just call wco.exe directly.
You need to escape your arguments just like you do on the command line plus you need to escape your backslashes:
string strCmdText = "wco -f \"C:\\Work\\6.70 ex\\Master Build.Txt\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
You will need to use the escape character, which is \ in C#.
string strCmdText = "wco -f \"C:\Work\6.70 ex\Master Build.Txt\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
However, if "wco" is an executable, the actual code you should use is
string strCmdText = "-f \"C:\Work\6.70 ex\Master Build.Txt\"";
System.Diagnostics.Process.Start("wco", strCmdText);
This will probably make it easier to redirect the output.
Related
I'm calling a PowerShell script from a console app. I need to call a script in a network share and pass a couple of arguments. I need help constructing the Arguments. I've tried precising the argument with an # symbol. here is a snipe of the code:
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "powershell.exe";
process.StartInfo.Arguments = "powershell.exe -ExecutionPolicy Bypass -Noninteractive -File "\\Share\ConfigScript.ps1" -Config "\\Share\Config.xml" -Webservice "https://companysite.net/ConfigSite"";
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
process.WaitForExit();// Waits here for the process to exit.
I get multiple CS1002 and CS1056 errors if I jusc copy/paste arguments the way they work in PowerShell.
EDIT: NOTE: powershell.exe in the argument is used to bypass Execution Policy
You need to escape the special characters in the string.
\\ will give you \
\" will give you "
Note these special characters are escaped using the \ character. Also, check if you intentionally meant to have powershell.exe in the arguments since it is already specified as the filename?
I want to close a mutant handle to run an application in 2 windows.
The handle name is:
\Sessions\3\BaseNamedObjects\Growtopia
I can close it in Process Hacker, but I can't figure out how to do it in C#.
I can close it by C# but its always different. Sometimes its 0x498, sometimes 0x49c and etc.
All code I got:
string path2 = Path.Combine(Path.GetTempPath(), "handle.exe");
File.WriteAllBytes(path2, RubbyEngine.Properties.Resources.handle);
Process.Start(path2, "-c 498 -y -p " + pidLabel.Text);
The -c 498 -y -p are the arguments I use to close the handle, but its always different, and I can't change it on every launch.
So im trying to run multiple commands in a one command line, im using WGET to download a few files to a local path this part works so far.
Here is what Ive got
startInfo.Arguments = #"/K wget -x -np -S -N -nH -r -R ""index.html*""
http://my-server.ip -P """ +
Properties.Settings.Default.userAddonDir+" && exit";
with this I should expect CMD to launch WGET with the command line:
-x -np -S -N -nH -r -R ""index.html*""
from my server:
http://my-server.ip
to my users local path for example:
E:/Folder/Sub Folder/
then once wget is finished it should see
&& Exit
and close cmd...
things to note, the users local path is likely to have white space so I've wrapped it in quotes to prevent the path being cut off. (hence all the quotes)
but what the path resolves to is odd. not to sure
E:/Folder/Sub Folder/ && exit/
If I place any more "" it breaks any ideas or pointers as im at a loss.
Thanks in advance.
Change your command to
string x = #"E:\temp";
string t = #"/K wget -x -np -S -N -nH -r -R ""index.html*"" http://my-server.ip -P """ +
x + #""" && exit";
After the operator + the initial verbatim character is no more effective. You need to reapply it to the last part of your string constant && exit
However, have you tried to launch your command with /C instead of /K?
This will automatically close the command window so you don't need anymore the Exit command
I'm trying to troubleshoot why the following function isn't working.
public void RunCmd()
{
string strCmdText;
strCmdText = "/C [enter command stuff here]";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
However whenever I try to run it or throw in some breakpoints, command opens, shows an error, and then closes really quickly (so quickly that I can't read anything).
Is there a way I can halt the program or figure out what's going on? Breakpoints don't seem to be working.
When I directly type it in Command Prompt instead of running it via this c# script, it the command works fine.
try this:
strCmdText = "/K [enter command stuff here]";
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
Maybe try adding a pause command?
There are a various options. Using /K will prevent the window from closing.
You can also edit your command to add a SLEEP after the main call. For example, the following will wait 2 seconds before exiting:
public void RunCmd()
{
string strCmdText = "/C \"[enter command stuff here]\" & \"SLEEP 2\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
Process.Start has two parameters there: the process name and the arguments to pass to the process. CMD is having a problem understanding what the argument "[enter" is. If it is not an argument CMD understands, it will immediately exit.
cin.get()
That waits for a keyboard press from the user.
create temp folder under c: then append " < c:\temp\log.txt" to the end of command. this writes the output to a file
In addition to the tip about using /K vs /C to create a shell that 'lingers', you'll probably want to inject an empty "set" command to see what environmental variables (and paths) are set in the spawned shell. In all likelihood, the difference between the successful run of your script in your own shell session and the one that fails in the spawned shell, is the environment settings in which it is run.
I am having an issue with the spaces in my command. I am trying to run the cmd prompt and execute a program that takes command line arguments. I need the cmd window to remain open after the process is finished executing.
I managed to get it working in another section of code, but this time i am almost sure it has to do with the spaces in the path of the argument. If i use a path with no spaces, it works fine. I tried to escape the quotes, but either i am doing it incorrectly, or escaping the quotes do not work.
Basically, I need to make the line below work with spaces and keep the cmd window open after the execution...
Dim ps As Process = System.Diagnostics.Process.Start("cmd /k", "C:\common\tools\tap.exe -f flash C:\Users\test project\Desktop\image.signed")
I'm know the space between "test" and "project" is the issue, but i haven't been able to get around it.
Thanks in advance for your help.
Wrap the path in double-quotes, like this:
"C:\common\tools\tap.exe -f flash ""C:\Users\test project\Desktop\image.signed"""
Giving you:
Dim ps As Process = System.Diagnostics.Process.Start ("cmd /k", "C:\common\tools\tap.exe -f flash ""C:\Users\test project\Desktop\image.signed""")