I'm trying to run this command with several quotation marks:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --proxy-server="10.10.10.10:9999" -user-data-dir=C:\filterbypass www.example.com"
I've tried adding backslashes to the before C and after com, no luck
Tried assigning it to a string
string \"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --proxy-server="10.10.10.10:9999" -user-data-dir=C:\filterbypass www.example.com\"
with hopes to run the command like that, but still nothing.
try
{
System.Diagnostics.Process.Start(#"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --proxy-server="10.10.10.10:9999" -user-data-dir=C:\filterbypass www.example.com");
}
catch { }
Can anyone show me where I'm going wrong here? Thanks,
You need to separate the binary to run from the arguments. I would recommend using ProcessStartInfo to make this clearer:
var info = new ProcessStartInfo
{
FileName = #"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
Arguments = "--proxy-server=10.10.10.10:9999 " +
#"--user-data-dir=C:\filterbypass " +
"www.example.com"
};
var process = Process.Start(info);
(Note the use of -- for the user-data-dir switch, by the way - it looks like all the command line flags are prefixed with --, not -. It's possible that it works both ways...)
Related
I have the follwoing c# code:
string cmdInnoSetup = #"""C:\Program Files (x86)\Inno Setup 5\compil32\""" + " /cc " + #"""c:\\SetupScript.iss""";
System.Diagnostics.Process.Start("cmd.exe","/k "+ cmdInnoSetup);
But when I execute it I got the following message:
"Program not found"
But when I remove double quotation from second part sentence "c:\\SetupScript.iss" then the code works fine!
Now how can I solve this illogical problem in order to keep both of paths inside double quotation !
How to let code work by setting double quotation for source and destination as well!
You can try using the string written like this:
"\"C:\\Program Files (x86)\\Inno Setup 5\\compil32\\\"" + " /cc \"c:\\SetupScript.iss\"";
Try this two lines, I tried it and it works:
string cmdInnoSetup = #"""C:\Program Files (x86)\Inno Setup 5\compil32.exe"" /cc 'c:\SetupScript.iss'";
System.Diagnostics.Process.Start("cmd.exe", "/k " + cmdInnoSetup);
You could try calling your program differently. Such as:
System.Diagnostics.Process.Start(#"C:\Program Files (x86)\Inno Setup 5\compil32.exe", #"/cc ""c:\SetupScript.iss""");
Unless you really need the cmd to perform the output it should do basically the same.
(I didn't try it with InnoSetup myself but with another program)
I've tried to do my due diligence in searching before I post but I am having trouble with a space in a file path when launching a powershell script in a C# app. I have tried escaping quotes, using # pretty much anywhere, single quotes. What am I missing here?
This is how I am defining the path:
public string importMusicLocation = #"C:\Program Files\Automation Toolbox\Scripts\Music_Import.ps1";
Then I launch a powershell process and point to the path, previously I had this working just fine without spaces when pointing to the path on my Desktop, but now that I want to put the application in Program Files the powershell window only detects the path up to the space.
private void LaunchPshell(string script) {
string strCmdText = Path.Combine(Directory.GetCurrentDirectory(), script);
var process = System.Diagnostics.Process.Start(#"C:\windows\system32\windowspowershell\v1.0\powershell.exe", strCmdText);
process.WaitForExit();
}
Like 5 minutes after I posted this I realized I don't need to be combining a directory, I was going to delete this but I can post the solution in case anyone else needs it. The general syntax for powershell to launch a script is & 'C:\Program Files\Some Path':
public string importMusicLocation = #"& 'C:\Program Files\Automation Toolbox\Scripts\Music_Import.ps1'";
and
private void LaunchPshell(string script) {
var process = System.Diagnostics.Process.Start(#"C:\windows\system32\windowspowershell\v1.0\powershell.exe", script);
process.WaitForExit();
}
Try quoting your path value with double quotes like
string importMusicLocation = #"""C:\Program Files\Automation Toolbox\Scripts\Music_Import.ps1""";
So, PowerShell.exe utility will receive the argument as quoted string and the spacing issue shouldn't arise
"C:\Program Files\Automation Toolbox\Scripts\Music_Import.ps1"
I'm trying to run a remote batch file - already located on the remote machine - using PsExec, called via Process in C#. I've confirmed that all required files already exist, but believe I may have a problem with my syntax, as the redirected output indicates that it can't find the file specified.
The machine against which PsExec runs is dynamic, which is the myArray[0].MachineName value (this pulls in without issue).
wsStopProcess.StartInfo.FileName = #"C:\Windows\system32\PsExec.exe";
wsStopProcess.StartInfo.Arguments = #" \\" + myArray[0].MachineName + #"D:\stopprofile.bat";
wsStopProcess.StartInfo.UseShellExecute = false;
wsStopProcess.StartInfo.CreateNoWindow = true;
wsStopProcess.StartInfo.RedirectStandardOutput = true;
wsStopProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
wsStopProcess.Start();
Any ideas on what appears to be formatted incorrectly? I'm guessing it's too many backslashes (or not enough!) somewhere.
I think the main problem is you do not have a space between the two arguments.
Try this:
wsStopProcess.StartInfo.Arguments = #"\\" + myArray[0].MachineName + #" D:\stopprofile.bat";
I would also warn you that I could not get psexec to work 100%, despite trying many different things.
Try this:
wsStopProcess.StartInfo.Arguments = #"\\" + myArray[0].MachineName + #" D$\stopprofile.bat";
So instead of using : try $ sign. Also setting breakpoint on the above line while debugging will help you to see the exact path.
I'm trying to compare two folders using a third party comparison tool called UltraCompare. The following line calls the program and opens two files... but this doesn't do anything except open them, plus it doesn't work properly for folders.
Process.Start("C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe",
textBoxContents1 + " " + textBoxContents2);
I'd like to use the following command line call which opens two folders, runs a comparison on them, and stores the results in output.txt: uc -d -dmf "c:\dir1" "c:\dir2" -o "c:\output.txt"
Also, I'd need to use variables for the folders instead of hardcoding the paths.
How can I work that into my C# code?
UPDATE 1:
I've modified my code according to your suggestions:
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe";
p.StartInfo.Arguments = String.Format("-d -dmf \"{0}\" \"{1}\" -o c:\\output2.txt",
textBoxContents1, textBoxContents2);
p.Start();
I'm wondering why the third line containing the arguments still doesn't work...
UPDATE 2:
My mistake. It is working now!! Just doesn't display the folders in UltraCompare but it is still writing and saving the output. Thanks guys!
You can use
yourProcess.StartInfo.Arguments = " .....";
Sample
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe";
p.StartInfo.Arguments = String.Format("-d -dmf {0} {1} -o c:\output.txt",textBoxContents1, textBoxContents2);
p.Start();
Process.Start(new ProcessStartInfo
{
FileName = #"C:\Program Files\IDM Computer Solutions\UltraCompare\uc.exe",
Arguments = String.Format("\"{0}\" \"{1}\"", textBoxContents1, textBoxContents2)
});
Make sure you use quotation marks for arguments, too!
If any of textBoxContents1 or textBoxContents2 contains spaces, you are busted!
I'm trying to copy a file over to a networked folder on a mapped drive. I tested out COPY in my command line which worked, so I thought I'd try automating the process within C#.
ProcessStartInfo PInfo;
Process P;
PInfo = new ProcessStartInfo("COPY \"" + "c:\\test\\test.txt" + "\" \"" + "w:\\test\\what.txt" + "\"", #"/Z");
PInfo.CreateNoWindow = false; //nowindow
PInfo.UseShellExecute = true; //use shell
P = Process.Start(PInfo);
P.WaitForExit(5000); //give it some time to finish
P.Close();
Raises an exception : System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
What am I missing? Would I have to add anything else to the command parameters?
I've tried File.Copy but it doesn't appear to work (File.Exists("<mappeddriveletter>:\\folder\\file.txt");) brings up false.
This SO post contains an example
Run Command Prompt Commands
how to do it right. You need to call cmd.exe with /c copy as a parameter.
Well, for the technical bit: copy in itself is not an executable, but merely a command interpreted by cmd. So basically, you'd have to start cmd.exe as a process, and pass it a flag that makes it run the copy command (which you'll also have to supply as a parameter).
Anyways, I'd side with Promit and recommend looking into File.Copy or something similar.
e: Ah, missed your comment on Promit's answer when I posted this.
Wouldn't it be a lot easier to use File.Copy ?