简体   繁体   中英

how to escape the space in Environment.SpecialFolder.ProgramFiles C#

i am trying to escape sequence the C:\\Program Files space. Originally i escaped it within a string

System.Diagnostics.Process.Start("XCOPY.EXE", "/E /I /Y \"" + ProgramFiles + "\\WinCon2\\*.*\" \"" + pfadauswahl + "\\Backup\\" + dt.ToString("yyyy-MM-dd") + "\\WinCon2\\\"");

Since i try to upgrade my program a bit to get rid of endless string chains, the new version looks like:

System.Diagnostics.Process.Start("XCOPY.EXE", "/E /I /Y " + pathWinCon2 + " " + backupPathWinCon2);

While in my first attempt the Program Files was escaped by hand

\"" + ProgramFiles + "\\WinCon2\\*.*\" \""

and the "C:\\Program Files" in a string...

Now i wanted to use either

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)

or

Environment.GetEnvironmentVariable("PROGRAMFILES")

but how can i escape them?

I have tried with this and it works as expected:

string s = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
string q = "Microsoft Sql Server";
string path = Path.Combine(s, q);
Process p = new Process();
p.StartInfo.FileName = "CMD.EXE";
p.StartInfo.Arguments = "/K DIR \"" + path + "\" /D /S";
p.Start();

Is this you want?

You can specify the path in quotes..

Since space separates different arguments, you have to give those paths in quotes.

pathWinCon2 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\WinCon2\*.*\";

backupPathWinCon2 = pfadauswahl + @"\Backup\" + dt.ToString("yyyy-MM-dd") + @"\WinCon2\";

System.Diagnostics.Process.Start("XCOPY.EXE", "/E /I /Y \"" + pathWinCon2 + "\" \"" + backupPathWinCon2 + "\"");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM