A little more investigation:
C:\Test\
is a normal directory
C:\Test\text.txt
is a normal text file.
C:\Text\text2.txt
is a 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