.net System.ArgumentException 路径中的非法字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/367797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:49:36  来源:igfitidea点击:

System.ArgumentException illegal characters in path

.netexceptionargumentexceptionillegal-characterspath-combine

提问by MOZILLA

I am using Path.Combine, and one of the strings contain a Unicode characters. I get {System.ArgumentException} exception; illegal characters in path.

我正在使用 Path.Combine,其中一个字符串包含一个 Unicode 字符。我收到 {System.ArgumentException} 异常;路径中的非法字符。

According to MSDNfilepath/name can have unicode characters. Why do I get this exception?

根据MSDN 文件路径/名称可以有 unicode 字符。为什么我会收到此异常?

Edit: Here is the code:

编辑:这是代码:

Path.Combine("C:\PDM\Silver","Amabel Bender QQQ")

Path.Combine("C:\PDM\Silver","Amabel Bender QQQ")

回答by MOZILLA

I figured out the problem. The second string contains a "tab" character in it causing the exception. (that didn't showed up when I pasted the string here)

我解决了这个问题。第二个字符串中包含导致异常的“制表符”字符。(当我在这里粘贴字符串时没有出现)

Thanks everyone and sorry for the confusion.

谢谢大家,很抱歉造成混乱。

回答by VonC

You may have double quotesin your set of paths, since Path.InvalidPathCharsincludes double-quotes.

您的路径集中可能有双引号,因为Path.InvalidPathChars包含双引号。

That would make your exception unrelated to your Unicode character...

这将使您的异常与您的 Unicode 字符无关......



Just saw your edit of your question: VB, no quotes.

刚刚看到您对问题的编辑:VB,没有引号。

So may be you have a non-printable character copied in your second string, which looks like a space, just before your 'QQQ' string ?
Try also if "Amabel BenderQQQ"or "AmabelBenderQQQ"work (just to test if it is related to 'QQQ' chars or to the space)

那么您可能在第二个字符串中复制了一个不可打印的字符,它看起来像一个空格,就在您的 'QQQ' 字符串之前?
也试试 if"Amabel BenderQQQ""AmabelBenderQQQ"work(只是为了测试它是否与“QQQ”字符或空间有关)

回答by DilbertDave

If you are using C# then I'm sure you need to escape the back-slashes

如果您使用的是 C#,那么我确定您需要转义反斜杠

Path.Combine("C:\PDM\Silver","Amabel Bender QQQ")

not applicable if you are using VB though...

如果您使用的是 VB,则不适用...