Unable to execute cmd command using C# - 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.

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.

How to avoid the space in specified location when build my source using c#?

I have tried to build a set of project file using c# code like as below code
string workingDirectory = #"C:\Windows\Microsoft.NET\Framework\v2.0.50727";
string arguments = string.Format("/c MSBuild {0} /p:Configuration=Release /t:rebuild /p:OutDir={1}", fileName);
ProcessStartInfo info = new ProcessStartInfo("cmd.exe", arguments);
But some project files having spaces in the path location. so build can be failed also tried to replace the spaces by %20 it is also not working.
so please suggest me a better solution to solve this.
Three ways to solve the spacing problem to command prompt:
cd "/path/path/path/A Folder/file"
cd '/path/path/path/A Folder/file'
cd /path/path/path/A\ Folder/file
Read the answer from #Benoit: https://askubuntu.com/a/530581
In C#,
1) Simply add " at the beginning and the end
2) Simply add ' at the beginning and the end
3) you could make use of string.Replace() to add \ in front of the space.
You're missing an argument to your String.Format().
It also looks like you should use escaped quotes \" to ensure your paths are quoted, like so:
string workingDirectory = #"C:\Windows\Microsoft.NET\Framework\v2.0.50727";
string arguments = string.Format("/c MSBuild \"{0}\" /p:Configuration=Release /t:rebuild /p:OutDir=\"{1}\"", fileName, workingDirectory);
ProcessStartInfo info = new ProcessStartInfo("cmd.exe", arguments);

how to give address of parts to copy command in c#

when i run copy command in my c# code it produce no error or exception because it is not finding the parts path i do not know how to give full directory path or path of every part which i am joining.actually i am merging file parts to a single file using coy/b by using this code...
string strCmdText;
strCmdText = "/C copy/b test.txt.10485760.0000.part" +
"test.txt.10485760.0001.part" +
"test.txt.10485760.0002.part" +
"test.txt.10485760.0003.part" +
"test.txt.10485760.0004.part" +
"test.txt.10485760.0005.part test.txt";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
You can specify the path of your files as the working path of the process. For example:
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = #"THE\PATH\OF\FILES",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
FileName = "cmd.exe",
Arguments = "YOUR COMAND HERE";
};
Process process = Process.Start(startInfo);
For your command, note that you can copy all files into one using a wildcard for the parts:
copy *.part test.txt
/b is for binary data, so i think is not needed in your case.
You can also set other properties for a process, for more info check the doc: ProcessStartInfo.
For such a complex task, I would use the process as a bash rather then just an execution tool.
Create a Process
Redirect it's streams (Input, Output)
With a StreamReader and StreamWriter u can now access the cmd
Now just control it the way u need it, like setting the working directory to the path and do ur command.
If this is not what u want, u can allways set the working directory on the ProcessStartInfor -> Link

Calling ProcessStartInfo with a path with spaces always breaks

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.

C# archiving using 7zip, keeps hanging on wildcard call

Hey all quick question for you. I am trying to archive a directory of files into a zip folder using 7zip as a system process but I am running into an odd error. Whenever I do a wildcard specification in my source file name such as "*.txt" it works fine. However, once I use the wildcard * around the actual file name (Which I need to and can't work around that), 7zip just seems to hang. I can see the archive directory get created in the folder but I can't open it and I have to close 7zip down via task manager.
Any advice?
Code is below:
public static void archiveFiles(string executionDirectory,string workDirectory,
string[] files)
{
string sourceName = #"C:\mypath\*testfile*"; <----// This seems to be my issue
string targetName = #"C:\\testcompress\archive.zip";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = #"C:\program files\7-zip\7z.exe";
p.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
You're most likely getting this issue because your path #"C:\mypath*testfile*" is a verbatim string, thus using the "*" character, or other such wildcards, are being taken as literal in the path instead of wildcards. Trying taking the # off the beginning of strings containing wildcards and that should fix your problem. You can find a more thorough explanation of what the "#" character, used in context of a prefix to strings, does here:
Verbatim Strings
I think you are having issues with your \'s as well as the fact that you should be using 7za.exe and not 7z.exe. Make sure your application has access to write to the directory in question.
string sourceName = #"C:\mypath\*testfile*";
string targetName = #"C:\testcompress\archive.zip";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = #"C:\program files\7-zip\7za.exe";
p.Arguments = string.Format ( "a -t7z {0} {1} -mx=9",targetName,sourceName);
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
console.writeline(string.format("7zip returned with exit code {0}",x.ExitCode));
7zip exit codes can be found here
Remember you can test it in a command prompt window to be sure that it works.
C:\program files\7-zip\7za.exe a -t7z C:\program files\7-zip\7za.exe C:\mypath*testfile* -mx=9

Categories

Resources