简体   繁体   中英

How do I replace PATH Environment variables to double quote + backslashes those which have spaces in C#?

Language version: C# - Operating system: Win 10 - IDE: Visual Studio

I've got a problem when letting the user set a path. When the path is without spaces, all good. No issues.

When the path is with spaces, the CLI i'm forwarding the data to, doesn't accept it and instantly closes.

That's because it misses the right syntax (double quotes at beginning & end + double backslashes) when it arrives. So the right syntax is:

app.exe -q MP3_128 -p "C:\\test test\\" 

Since I'm using the Environment.GetFolderPath in WMF, I have no clue how to add these to its output...

How do I replace PATH Environment variables to double quote + backslashes those which have spaces in C#?

Code:
if (Settings.Default.download == "")
{
MessageBox.Show("Be sure your download path doesnt contain any spaces!");
String path = Environment.GetFolderPath (Environment.SpecialFolder.MyMusic);
String pathDouble = path.Replace("'", "\"");
Settings.Default.download = @"""C:\\Test\DOWN LOADS""";
Settings.Default.Save();
}

which been reached here in

Code:
if (Settings.Default.sm != "")
{
download.StartInfo.FileName = Settings.Default.sm;
string a = " -q " + qualitys + " -p " + Settings.Default.download + " " + 
info[result.SelectedIndex].link;
Debug.Write(a);
download.StartInfo.Arguments = a;
}

The hardcoded default path set works. But when the user changes that path using the GUI to their own likings, it's gone.

I think I'm editing the wrong code.

This code, I have edited, which only now needs a double \\ after the drive:

using (var folderDialog = new FolderBrowserDialog())
        {
            if (folderDialog.ShowDialog() == DialogResult.OK)
            {

                Settings.Default.download = @"""" + folderDialog.SelectedPath + @"\\""";
                Settings.Default.Save();
                txb_download_folder.Text = Settings.Default.download;
            }
        }

But how?

A simple \\ extra in the first @"\\" results in \\"C:\\test test\\\\".

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