Reputation: 588
I am trying to pass path string as arguments to windows form application. I understand that I need add the quotes. I am currently using below code.
DirectoryInfo info = new DirectoryInfo(path);
string.Format("\"{0}\"", info.FullName);
The code above works when path is like D:\My Development\GitRepositories
. However when I pass C:\
the argument I get is C:"
because last \
character working as escape character.
Am I doing something wrong? Also, is there a better way to do this?
Thanks in advance.
Upvotes: 3
Views: 27472
Reputation: 870
Try using ProcessStartInfo and the Process class and spawn your application. This will also give you much more control over how it is launched and any output or errors it returns. (not all options are shown in this example of course)
DirectoryInfo info = new DirectoryInfo(path);
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = [you WinForms app];
processInfo.Arguments = String.Format(@"""{0}""", info.FullName);
using (Process process = Process.Start(processInfo))
{
process.WaitForExit();
}
Upvotes: 2
Reputation: 47945
Your problem is escaping in C# you could mask all backslashes with a second backslash or put an at sign (@) before the first quote:
string option1="c:\\your\\full\\path\\";
string option2=@"c:\your\full\path\";
Anyway not in every case are quotes into a string nessesary. In most cases just if you need to start an external programm and this only if you need this as an argument.
Upvotes: 2
Reputation: 20575
CommandLineArg
are space
delimited, hence u need to pass the command-arg with "
which mean if Path = C:\My folder\
will be sent as two argument, but if it passed as "C:\My Folder\"
it is a single argument.
so
string commandArg = string.Format("\"{0}\"", info.FullName)
Upvotes: 1