Passing path as arguments

22,323

Solution 1

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();
}

Solution 2

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.

Solution 3

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)
Share:
22,323
utkarsh
Author by

utkarsh

Microsoft ALM MVP | ALM Ranger | Love everything about Visual Studio Follow me at: Twitter Blog

Updated on February 12, 2020

Comments

  • utkarsh
    utkarsh over 4 years

    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.