0

我收到“NotSupportedExeption 未被用户代码处理 - 不支持指定的路径格式”错误,即使我按照要求使用字符串。

string path = folder + "/" + filename;

fileByte = File.ReadAllBytes(path); // error here

知道问题是什么吗?

将代码编辑为此

string path = Path.Combine(folder, filename);

fileByte = File.ReadAllBytes(path);

路径是“F:\Web\Opgaver\Skirmer\Hjemmesiden\BETA\Skirmer 17-04-2012\Skirmer 17-04-2012\Billeder\Galleri\F:\Web\Opgaver\Skirmer\Hjemmesiden\BETA\Skirmer 17 -04-2012\Skirmer 17-04-2012\Billeder\Galleri\2011\Vingsted\DSC_0001.JPG"

错误仍然发生。我看到的是 ReadAllBytes 需要一个显示路径的字符串,我得到了,但它仍然显示错误

4

5 回答 5

2

不应/在路径中使用,因为斜杠在 Windows 中是无效字符。用于Path.Combine创建它:

string path = Path.Combine(folder, filename);
于 2012-09-25T07:41:06.110 回答
1

我认为您想使用反斜杠,或者更确切地说Path.DirectorySeparatorChar是返回正确分隔符的属性,而不管文件系统如何:

string path = folder + Path.DirectorySeparatorChar.ToString() + filename;

或者您可以使用以下Path.Combine方法:

string path = Path.Combine(folder, filename);
于 2012-09-25T07:41:55.177 回答
0

变量的确切值是path多少?此外,您应该使用Path.Combine将路径部分连接成完整路径。

作为File.ReadAllBytes状态的文档:

 NotSupportedException  - path is in an invalid format. 
于 2012-09-25T07:41:17.470 回答
0

您的路径格式不正确:

NotSupportedException  path is in an invalid format. 

MSDN:system.io.file.readallbytes

于 2012-09-25T07:41:36.127 回答
0

如果您在编辑的问题中发布的路径确实是您尝试从中读取的路径,那么您收到异常的原因是因为路径中有两个冒号。驱动器号重复两次 ( F:\...F:\...)。

folder您最终选择该路径的原因完全取决于filename您对Path.Combine(). folder两者都不太可能filename从完整路径开始,因为在这种情况下Path.Combine()filename作为组合路径返回。很可能您的folder变量已经包含基本路径的两个副本,带有两个驱动器号,因此有两个冒号,因此NotSupportedExeption在您调用Path.Combine().

于 2016-08-17T09:07:48.450 回答