Directory.GetFiles () gives strange return values

This problem occurs when I try to hide a file in my directory using the File Lock tool . This is no ordinary spy as I cannot see it in Windows Explorer. The code:

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

      

How can the second function return "c: \ mydir \". file? I can delete all files that do not exist to solve my problem, but I am wondering why I am getting "." file in the first place.

0


a source to share


3 answers


I have no experience with the File Lock tool, but I can assume that it intercepts the WinAPI FindFirstFile function to protect some file from listing (this function is used by the .NET Directory.GetFiles () function). And this hook can be written a bit ... buggy :)



Could you try to turn off the tool and repeat the test?
Most likely, you will solve the problem.
But if you get the same result, blame Directory.GetFiles ().

+3


a source


A little more research:

  • C:\Test\

    is a regular directory
  • C:\Test\text.txt

    - a plain text file.
  • C:\Text\text2.txt

    - text file hidden by file lock

The following code ...



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));
    }
  }
}

      

... outputs:

########### 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


a source


"" refers to the current directory. Try:

Directory.Exists(textFiles[0]);

      

to see it in action.

Why the second overload returns its record and the first does not, I don't know.

0


a source







All Articles