Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文件路径中的空格不能与@或\“一起使用;(C#)_C#_Powershell_Path_Filepath - Fatal编程技术网

文件路径中的空格不能与@或\“一起使用;(C#)

文件路径中的空格不能与@或\“一起使用;(C#),c#,powershell,path,filepath,C#,Powershell,Path,Filepath,在发布之前,我已经尽了最大努力进行搜索,但在C#应用程序中启动powershell脚本时,我遇到了文件路径中有空格的问题。我尝试过转义引号,几乎在任何地方都使用@单引号。我错过了什么 这就是我定义路径的方式: public string importMusicLocation = @"C:\Program Files\Automation Toolbox\Scripts\Music_Import.ps1"; 然后我启动一个powershell进程并指向路径,以前我在指向桌面上的路径时没有空格,

在发布之前,我已经尽了最大努力进行搜索,但在C#应用程序中启动powershell脚本时,我遇到了文件路径中有空格的问题。我尝试过转义引号,几乎在任何地方都使用@单引号。我错过了什么

这就是我定义路径的方式:

public string importMusicLocation = @"C:\Program Files\Automation Toolbox\Scripts\Music_Import.ps1";
然后我启动一个powershell进程并指向路径,以前我在指向桌面上的路径时没有空格,但现在我想将应用程序放入程序文件中,powershell窗口只检测到空间上的路径

private void LaunchPshell(string script) {
    string strCmdText = Path.Combine(Directory.GetCurrentDirectory(), script);

    var process = System.Diagnostics.Process.Start(@"C:\windows\system32\windowspowershell\v1.0\powershell.exe", strCmdText);
    process.WaitForExit();
}
在我发布这篇文章5分钟后,我意识到我不需要合并一个目录,我打算删除它,但我可以发布解决方案,以防其他人需要它。powershell启动脚本的一般语法是
&“C:\Program Files\Some Path”

public string importMusicLocation = @"& 'C:\Program Files\Automation Toolbox\Scripts\Music_Import.ps1'";


尝试用双引号引用路径值,如

string importMusicLocation = @"""C:\Program Files\Automation Toolbox\Scripts\Music_Import.ps1""";
因此,
PowerShell.exe
实用程序将以带引号的字符串形式接收参数,并且不应出现间距问题

"C:\Program Files\Automation Toolbox\Scripts\Music_Import.ps1"

这是有效的代码,但是它实际上并没有在powershell中执行脚本。输出:
PS C:\WINDOWS\system32>“C:\Program Files\Automation Toolbox\Scripts\Music\u Import.ps1”C:\Program Files\Automation Toolbox\Scripts\Music\u Import.ps1
@nkasco prepend
&
您的问题是什么?顺便说一句,如果文件或文件夹的名称中有两个或多个相邻空格,则使用
@“&C:\Program Files\Automation Toolbox\Scripts\Music\u Import.ps1”将失败。
"C:\Program Files\Automation Toolbox\Scripts\Music_Import.ps1"