Directory.GetFiles()给出了奇怪的返回值

时间:2009-05-13 08:02:31

标签: c# .net file io locking

当我尝试使用名为File Lock的工具隐藏目录中的文件时,会出现此问题。这不是常规隐藏,因为我在Windows资源管理器中看不到它。 代码:

string[] textFiles = Directory.GetFiles(@"c:\mydir")
//0 files returned

string[] textFiles = Directory.GetFiles(@"c:\mydir", "*.txt")
//1 file returned: "c:\mydir\."
File.Exists(textFiles[0])
//false

第二个函数如何返回“c:\ mydir \”。文件?我可以删除所有不存在的文件来解决我的问题,但我想知道为什么我会得到一个“。”文件首先。

3 个答案:

答案 0 :(得分:3)

我没有文件锁工具的经验,但我可以假设它挂钩FindFirstFile WinAPI函数来保护某些文件不被列出(该函数由.NET Directory.GetFiles()函数使用)。这个钩子可以写得有点......马车:))

您可以尝试禁用该工具并重做测试吗? 你最有可能解决这个问题 但如果得到相同的结果,请责备Directory.GetFiles()。

答案 1 :(得分:2)

再多一点调查:

  • C:\Test\是一个普通目录
  • C:\Test\text.txt是普通文本文件。
  • C:\Text\text2.txt是由File Lock隐藏的文本文件

以下代码......

using System;
using System.IO;

public static class GetFilesTest {
  public static void Main() {
    int counter = 0;
    DirectoryInfo dir = new DirectoryInfo(@"C:\Test");
    foreach (FileSystemInfo fsi in dir.GetFileSystemInfos("*.txt")) {
      Console.WriteLine("########### FileSystemInfo {0} ###########", ++counter);
      Console.WriteLine("fsi: {0}", fsi);
      Console.WriteLine("Exists: {0}", fsi.Exists);
      Console.WriteLine("FullName: {0}", fsi.FullName);
      Console.WriteLine("Name: {0}", fsi.Name);
      Console.WriteLine("Extension: {0}", fsi.Extension);
      Console.WriteLine("Attributes: {0}", fsi.Attributes);
    }

    counter = 0;
    string[] files = {@"C:\Test\test.txt", @"C:\Test\test2.txt"};
    foreach(string file in files) {
      FileSystemInfo fi = new FileInfo(file);
      Console.WriteLine("########### FileInfo {0} ###########", ++counter);
      Console.WriteLine("fi: {0}", fi);
      Console.WriteLine("Exists: {0}", fi.Exists);
      Console.WriteLine("FullName: {0}", fi.FullName);
      Console.WriteLine("Name: {0}", fi.Name);
      Console.WriteLine("Extension: {0}", fi.Extension);
      Console.WriteLine("Attributes: {0}", fi.Attributes);
      Console.WriteLine("Contents: {0}", File.ReadAllText(fi.FullName));
    }
  }
}

......输出:

########### FileSystemInfo 1 ###########
fsi: test.txt
Exists: True
FullName: C:\Test\test.txt
Name: test.txt
Extension: .txt
Attributes: Archive
########### FileSystemInfo 2 ###########
fsi: .
Exists: False
FullName: C:\Test\.
Name: .
Extension: .
Attributes: Directory
########### FileInfo 1 ###########
fi: C:\Test\test.txt
Exists: True
FullName: C:\Test\test.txt
Name: test.txt
Extension: .txt
Attributes: Archive
Contents: test
########### FileInfo 2 ###########
fi: C:\Test\test2.txt
Exists: True
FullName: C:\Test\test2.txt
Name: test2.txt
Extension: .txt
Attributes: Archive

Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'C:\Test\test2.txt' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
   at System.IO.StreamReader..ctor(String path, Encoding encoding)
   at System.IO.File.ReadAllText(String path, Encoding encoding)
   at System.IO.File.ReadAllText(String path)
   at GetFilesTest.Main() in c:\Users\Yvonne\Temp\GetFilesTest.cs:line 29

答案 2 :(得分:0)

“”。指的是当前目录。尝试:

Directory.Exists(textFiles[0]);

看到它在行动。

为什么第二次重载会返回进入而第一次没有,我不知道。