5

In .NET i'm running this line

var p = Process.Start(@"cmd", @"/C mklink /H c:\z\b c:\z\a\");

This works all fine however I'm worried that if one of the two args for mklink has a space that this wont work correctly. So I added "" around both parameters. Executing this line no longer worked and when I wrote \"" instead it still didn't work.

How do I write quotes when I am executing cmd /C?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • ah ha funny, after implementing this the very first entry in the db has a space (folder name). –  Nov 30 '12 at 22:25
  • You probably want to enclose the entire mklink command (with its arguments) with quotation marks, and then also the individual paths (escaping and double-escaping as appropriate). – isturdy Nov 30 '12 at 22:30
  • @isturdy: I'm not exactly sure what you mean. I don't exactly know what to escape except Roberts current answer doesnt appear to work for me –  Nov 30 '12 at 23:43

1 Answers1

4
string sourcePath = @"c:\z\b";
string targetPath = @"c:\z\a";

string arguments = string.Format("\"{0}\" \"{1}\"", sourcePath, targetPath);

var p = Process.Start("cmd", "/C mklink /H " + arguments);

Working example:

string sourcePath = @"c:\documents and settings\harvey robert\My Documents\Test.txt";
string targetPath = @"c:\test";

string s = string.Format("\"{0}\" \"{1}\"", sourcePath, targetPath);
Process.Start("cmd", @"/c copy " + s);

1 files copied.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • I havent tried this but it looks incorrect. I wrote @"/C mklink /H ""{0}"" ""{1}""" and got an error of incorrect syntax –  Nov 30 '12 at 23:42
  • The line you wrote isn't going to work; the @ sign disables the \ escapes. Try `var p = Process.Start(@"cmd", @"/C mklink /H " + "\"" + @"c:\z\b" + "\" \" " + @"c:\z\a\" + "\"");` – Robert Harvey Nov 30 '12 at 23:44
  • 1
    Why would i write it like that? `\"` simply is `""` when it is in a string with the `@` prefix –  Nov 30 '12 at 23:46
  • You didn't try it? :( Try it... It's clean, and the absence of the multiple quotation marks running together keeps my head from exploding. – Robert Harvey Nov 30 '12 at 23:47
  • Isn't this what you're telling me to do? `var args = string.Format(@"/C mklink /H ""{0}"" ""{1}""", newfn, target);` But it seems like you're suggesting to do wit using "text \" " instead of doing what i currently do to add quotes –  Nov 30 '12 at 23:48
  • I'm pretty sure the `{0}` won't work with the `@literal` syntax. – Robert Harvey Nov 30 '12 at 23:49
  • It does. I have always used `{0}` with string. The result in my first entry is `/C mklink /H "J:\test dir_1/00000000-0000-0000-0000-000000000000" "J:\test dir/file.dat"` –  Nov 30 '12 at 23:52
  • Nope and thats why i wrote the question. And thats why i thought it was weird and tried using `\"` (well `\""` in my code) anyways. Still no luck –  Nov 30 '12 at 23:57
  • Actually maybe it does work. It might be a permission problem. Visual studios doesnt have the same permission as i do even though i'm sure no admin permissions are required. I just checked the standard error. –  Nov 30 '12 at 23:59
  • You're completely right. It looks like weirdness with permission on external HD. This seems to works perfectly fine when the files are on my local drive. Ah ha, I actually just didnt see the file after running it the first time then getting file already exist on other runs. The link has the same modified date and since the filename started with many 0s and this directory has lots of files that start with multiple 0s I didnt see it while typing nor looking at recently modified. –  Dec 01 '12 at 00:05