简体   繁体   English

我该如何替换PATH环境变量以在C#中使用空格将那些变量加双引号和反斜杠?

[英]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 语言版本:C#-操作系统: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. 当路径中有空格时,CLI将数据转发到该CLI,不接受它,然后立即关闭。

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... 由于我在WMF中使用Environment.GetFolderPath,因此我不知道如何将这些添加到其输出中...

How do I replace PATH Environment variables to double quote + backslashes those which have spaces in C#? 我该如何替换PATH环境变量以在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. 但是,当用户使用GUI根据自己的喜好更改该路径时,它就消失了。

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\\\\". 第一个@“ \\”中的简单\\ extra导致\\“ C:\\ test test \\\\”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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