Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing path as arguments

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.

like image 513
utkarsh Avatar asked Jan 24 '26 15:01

utkarsh


2 Answers

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.

like image 53
rekire Avatar answered Jan 26 '26 05:01

rekire


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();
}
like image 36
Darrin Doherty Avatar answered Jan 26 '26 03:01

Darrin Doherty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!