【发布时间】:2012-11-11 22:09:39
【问题描述】:
正如标题所解释的,我有一个程序可以在继续之前检查目录是否存在。
当检查完成时,它说目录不存在!
这是存储目录路径的代码:
string currentDirectory = Path.GetDirectoryName(Application.ExecutablePath);
Console.WriteLine("----" + currentDirectory.ToString());
string tesseractPath = Path.Combine(currentDirectory, @"..\..\..\tesseract");
_wrapper = new AsyncTesseractWrapper(tesseractPath);
public TesseractWrapper(string programLoc)
{
DirectoryInfo dinfo = new DirectoryInfo(programLoc);
//DirectoryInfo dinfo = new DirectoryInfo("C:\\Windows");
ValidateTesseractDirectory(dinfo);
_tesseractLocation = dinfo.FullName;
}
以及执行检查的代码:
private void ValidateTesseractDirectory(DirectoryInfo dinfo)
{
if (!dinfo.Exists)
throw new ArgumentException("Specified program directory must exist.");
FileInfo[] files;
files = dinfo.GetFiles(_tessExe);
if (files.Length != 1)
throw new ArgumentException("Specified program directory must contain tesseract.exe.");
}
我已尝试使用多种变体进行调试,例如检查 C:\Windows 文件夹是否存在,但它仍然给我一个错误...
代码有问题,还是我对 .Exists 方法的理解有问题...?
谢谢!
【问题讨论】:
-
我没有发现任何明显的错误。
Directory.Exists()返回什么?你的环境有什么不寻常的地方吗,比如虚拟机? -
tesseractPath的实际计算值是多少? -
.Exist 返回一个布尔值(取决于它是否存在)。而且没有虚拟机。虽然我刚刚意识到我应该尝试在一个干净的项目上测试相同的方法......但这也没有什么问题。
-
@SteveB 如果我
console.writeline它,这就是我得到的(有点长):C:\Users\Sulaiman\Desktop\Programming\Visual Studio 2010\Projects\PDF_Image_to_Excel_Converter\Image-OCR\ Atlasoft\TesseractLancher Files\tesseract 而且确实存在。我可以将其复制/粘贴到资源管理器中,然后文件夹就会打开。 -
@Sulaiman.89 如果您的路径已经很长,并且您正在从四个目录运行代码,您可能会遇到最大路径长度问题。 Win7 的最大值为 260。尝试将您的项目移动到 c:\MySweetProject\,看看是否得到相同的结果。
标签: c# file directory exists filesysteminfo