正如标题所解释的那样,我有一个程序可以在继续之前检查目录是否存在。
当检查完成时,它表示该目录不存在!
以下是存储目录路径的代码:
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方法的理解......?
谢谢!
答案 0 :(得分:3)
可能是因为权限问题。引用MSDN:
如果在尝试确定指定文件是否存在时发生任何错误,则Exists属性返回false。在引发异常的情况下会发生这种情况,例如传递带有无效字符或字符太多的文件名,磁盘失败或丢失,或者调用者没有读取文件的权限。
答案 1 :(得分:0)
我认为问题在于微软已经改变了文件夹的结构,并且“很明显”他们的工作人员仍然在寻找'好老路'。 在过去一个文件夹曾经有'..'这是'文件夹标记'(dos当然)我可以看到这不再存在。 我做了什么:我把一个虚拟文件放入/复制到一个新文件夹,一个图像或其他什么,而不是使用'目录'我使用file.exists 我认为答案可能在属性中。
答案 2 :(得分:0)
实际上是一样的。这是因为使用软链接(带有有关文件夹信息的文本文件)而不是联结。
首先递归加载文件夹,然后加载其文件并修复错误的连接。长文件名等
public static List<string> GetFilesEveryFolder(string folder, string mask, SearchOption searchOption, bool _trimA1 = false)
{
List<string> list = new List<string>(); ;
List<string> dirs = null;
try
{
dirs = GetFoldersEveryFolder(folder, "*").ToList();
}
catch (Exception ex)
{
throw new Exception("GetFiles with path: " + folder, ex);
}
foreach (var item in dirs)
{
try
{
list.AddRange(Directory.GetFiles(item, mask, SearchOption.TopDirectoryOnly));
}
catch (Exception ex)
{
// Not throw exception, it's probably Access denied on Documents and Settings etc
//ThrowExceptions.FileSystemException(type, RH.CallingMethod(), ex);
}
}
CA.ChangeContent(list, d => SH.FirstCharLower(d));
if (_trimA1)
{
list = CA.ChangeContent(list, d => d = d.Replace(folder, ""));
}
return list;
}