我正在尝试访问主文件夹中存在的子文件夹中的所有文件,以及它们的最后访问时间。
我能够获取所有文件,当我尝试获取最后访问时间时会出现问题。
我收到“对象未设置为对象的实例”异常。
我无法在我的代码中看到异常。
下面是代码:
public List<String> GetDirs(string Dir)
{
List<String> files = new List<String>();
try
{
foreach (string f in Directory.GetFiles(Dir))
{
files.Add(f);
}
foreach (string d in Directory.GetDirectories(Dir))
{
files.AddRange(GetDirs(d));
}
int num=files.Count;
FileInfo[] fileinfo=null;
for (int i = 0; i < num; i++)
{
fileinfo[i] = new FileInfo(files[i]);//Exception at this line of code
}
foreach(FileInfo xx in fileinfo)
{
Response.Write(xx.LastWriteTime);
}
}
catch (System.Exception excpt)
{
Response.Write(excpt.Message);
}
return files;
}
代码被称为:
string fullPath = @"D:\Main\Folder1";
List<string> lst=GetDirs(fullPath);
代码暂时不完整。
Folder1里面有3个文本文件,比如:
D:\Main\Folder1\a.txt
D:\Main\Folder1\bin\a1.txt
D:\Main\Folder1\SubFolder1\sa1.txt
我也在尝试获取上述文件的最后访问时间,但例外情况不允许更进一步。