当我在我的 ASP.NET MVC 应用程序中使用具有特定路径(比如 @"C:\")的 DirectoryInfo 时,它返回正常,但是当我尝试在我的外部 C# 库中使用完全相同的路径时,它抛出以上异常。我已经检查了路径一千次,路径是合法的。有人能告诉我原因吗?
编辑: 这是代码:
var di = new DirectoryInfo("C:\\App\\Files\\");
// var file = di.GetFiles(Id + ".*").First();
// if (file != null) return file.FullName;
// return string.Empty;
以上代码在一个属性中使用。
谢谢。
最佳答案
来自文档:
path contains invalid characters such as ", <, >, or |.
http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.directoryinfo.aspx
看看你的实际路径是否有这些。我知道你说你已经检查过路径是否合法,但这是唯一的情况。也许在这里给出确切的路径会有所帮助。
编辑:
使用 Path.GetInvalidPathChars() 和 Path.GetInvalidFileNameChars() 并查看是否添加了任何非法内容。
关于c# - DirectoryInfo 为正确的路径抛出 "Argument Exception: The path is not of a legal form",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5705250/