Calling ProcessStartInfo with a path with spaces always breaks - c#

I'm calling a command line program with the following configuration:
var processStartInfo = new ProcessStartInfo {
FileName = mainCommand,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
};
And when I'm running it with mainCommand being a path that has no spaces it always works, if there is a space on the path to the command it fails with:
Could not find the command file at C:\Users\Some
Where the actual path would be:
C:\Users\Some User\AppData\Local\Temp\Process.exe
So, why isn't it being escaped and is there a way I can escape this path name to prevent this error?

Try wrapping it with quotes:
string targetExe = "\"C:\\this path with spaces\\helloWorld.exe\"";
It works like such, but it also work without having to worry about it as Patrick Hofman said. Something's different on your system it seems.
If you want to pass arguments, do it trough Arguments in ProcessStartInfo. Obviously, if these have spaces too (ie: /arg1 "An Argument"), you will have to wrap them in quotes as above.

Related

Pass path with spaces to Fortran application

I have an old Fortran application to which I'd like to pass a path to a file.
Unfortunately my path contains spaces. I cannot change the file path but I'd like to process the file with my Fortran application.
The Fotran app doesn't work even if I pass the path from C# with quotes (e.g. "C:\Users[username]\Desktop\Example Path\example_file.dat").
This is my code:
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = #"/c <program_name> <file path with spaces>",
WorkingDirectory = #"C:\Users\<username>\Desktop\<exe_location>\",
UseShellExecute = false,
CreateNoWindow = true
};
This is the error, stopping at the first space:
Non existent file: <file p
It's the same if I try to pass the path via command line.
Is there some escape sequence I'm missing? Thank you in advance.

Unable to execute cmd command using C#

I have been trying to invoke below cmd command from C#, but it didn't worked and I got wrong path error. Although it is working if I execute it directly from CMD:
CMD Command: C:\Program Files (x86)\ABC Client>xyz.exe /launch "Your Software 12.7"
I tried below code:
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd")
{
WorkingDirectory = #"C:\Windows\System32",
Arguments = "/C \"\"C:/Program Files (x86)/ABC Client/xyz.exe\"\" /launch 'Your Software 12.7'",
RedirectStandardOutput = true,
RedirectStandardError = true,
WindowStyle = ProcessWindowStyle.Normal,
UseShellExecute = false
};
Process process = Process.Start(processStartInfo);
You need to escape the quotation marks.
This question is about escaping quotation marks
string softwareName = "\"Your Software 12.7\"";
This should do the trick.
Finally fixed: The correct string will be:
Arguments = "/C \"\"C:/Program Files (x86)/ABC Client/xyz.exe\" /launch \"Your Software 12.7\"\"";
Thank you everyone for your inputs :)
If the problem is just the string to be executed, i think this outputs just what you want:
Arguments=#"C:\Program Files (x86)\ABC Client\xyz.exe /launch ""Your Software 12.7""";
Either of the following should work:
Arguments = #"/C ""C:\Program Files (x86)\ABC Client\xyz.exe"" /launch ""Your Software 12.7""";
Arguments = "/C \"C:\\Program Files (x86)\\ABC Client\\xyz.exe\" /launch \"Your Software 12.7\"";
That is, double quotes around the program location (you had double quotes twice instead of once), back slashes instead of forward slashes, and double quotes around the "Your Software 27.7".
Using a string literal (# prefix) you need a double quote before each double quote in the final string. Without the # prefix, you need a back slash before each back slash and double quote in the final string.

Executing batch file in WCF

I am trying to execute a batch file server-side in IIS to add a printer using the printuientry call.
The problem I am facing is that I am using the Copy To Output Directory - Copy Always and the following code:
var path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
var processInfo = new ProcessStartInfo(Path.Combine(path, "AddPrinter.bat"))
{
CreateNoWindow = true,
UseShellExecute = false,
WorkingDirectory = path,
Arguments = ipAddress,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
var process = Process.Start(processInfo);
process.WaitForExit(10000);
process.Close();
Now when deugging, I have checked the values of path and its set to
file:\C:\_Projects\PrinterServerV2\bin
and I have checked to see if the file and directory exist which they do.
But I get the exception:
System.ComponentModel.Win32Exception (0x80004005): The directory name is invalid
Any ideas please??
Check if the user you had set in the iis configuration does have all privileges to run, access, write and read what you wanna do with your batch file.
Also try to change your ProcessStartInfo like the process will be cmd.exe and your batch file the argument.
I had a similar issue How to execute multiples .BAT files in C#
try AppDomain.CurrentDomain.BaseDirectory as path.

Process.StandardOutput.ReadToEnd() and extraneous newline chars

I am trying to understand why, when I call the above function, I am getting hex 0D0A every 80th column on the output I am reading.
I have a powershell script, for testing that has two lines in it for brevity's sake:
$xmlSpew = "<IISAppPoolConfig><DefaultWebSite><ApplicationPoolName>DefaultAppPool</ApplicationPoolName></DefaultWebSite><AuthN>Basic</AuthN></IISAppPoolConfig>"
Write-Output $xmlSpew
I am calling the script using the Process object with ProcessStartInfo as follows:
var psi = new ProcessStartInfo
{
WorkingDirectory = Path.GetDirectoryName(FileToRun),
FileName = FileToRun,
Arguments = Arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
};
var process = new Process
{
StartInfo = psi,
EnableRaisingEvents = true,
};
FileToRun value is:
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe
Arguments value is:
-File "C:\Program Files\MyTest\MyProgInputs\read.d\IISAppPoolConfig.ps1"
The script runs fine and I get back exit code 0, but I have this mysterious (to me) 0D0A newline every 80th char in standard out that I capture using:
var Stdout = new List<string>;
...
Stdout.Add(process.StandardOutput.ReadToEnd());
This is wreaking havoc on my XML efforts once I have standard out stored in a string var. I expected to get exactly what I write to the stdout in the ps1 script, not the extra newline.
What am I missing? I've looked for others with this issue, but I have not found an answer. Hopefully it is not me being search-challenged.
Follow this P/Invoke method and set dwXCountChars to a very large value. Don't forget to include STARTF_USECOUNTCHARS in the flags as well.
Final and tested resolution for now (because I need to ship something), is to have output from powershell come from the Write-Host cmdlet instead of Write-Output. The process for obtaining stdout remained the same as in my original post. Hope this helps others. Thanks for all the inputs.

batch file execution in c#

I am running a Java batch file from C#. If I run it by double clicking it executes successfully, but if I run it from C# code it gives exception in thread
"exception in "main" thread
java.lang.noclassdeffoundError"..
what can be the reason and how can it be solved? I am using the code:
var si = new ProcessStartInfo();
si.CreateNoWindow = true;
si.FileName = "batch-file path";
si.UseShellExecute = true;
Process.Start(si);
You are most likely missing some of the parameters that would be included in your systems environment variables.
Try setting working directory like this
process.StartInfo.WorkingDirectory = "C:\";
Also, try few other options as mentioned here,
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/20992653-dabf-4b31-85f7-e7bfbfb4557c
Try adding the following code as the first line to your batch file.
#cd /d %~dp0
Do not use batch_process_path + "\" + instead use Path.Combine() to make sure the path is correctly fitted with slashes.
Also read this "When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable"
So set it to false.

Categories

Resources