展开全部
//3.---------遍历文件夹中的文件和子文件夹并显示其属性---------
if(Directory.Exists(Server.MapPath("a")))
{
//所有子文件夹
foreach(string item in Directory.GetDirectories(Server.MapPath("a")))
{
Response.Write("<b>文件夹:" + item + "</b><br/>");
DirectoryInfo directoryinfo = new DirectoryInfo(item);
Response.Write("名称:" + directoryinfo.Name + "<br/>");
Response.Write("路径:" + directoryinfo.FullName + "<br/>");
Response.Write("创建时间:" + directoryinfo.CreationTime + "<br/>");
Response.Write("上次访问时间:" + directoryinfo.LastAccessTime + "<br/>");
Response.Write("上次修改时间:" + directoryinfo.LastWriteTime + "<br/>");
Response.Write("父文件夹:" + directoryinfo.Parent + "<br/>");
Response.Write("所在根目录:" + directoryinfo.Root + "<br/>");
Response.Write("<br/>");
}
//所有子文件
foreach (string item in Directory.GetFiles(Server.MapPath("a")))
{
Response.Write("<b>文件:" + item + "</b><br/>");
FileInfo fileinfo = new FileInfo(item);
Response.Write("名称:" + fileinfo.Name + "<br/>");
Response.Write("扩展名:" + fileinfo.Extension +"<br/>");
Response.Write("路径:" + fileinfo.FullName +"<br/>");
Response.Write("大小:" + fileinfo.Length +"<br/>");
Response.Write("创建时间:" + fileinfo.CreationTime +"<br/>");
Response.Write("上次访问时间:" + fileinfo.LastAccessTime +"<br/>");
Response.Write("上次修改时间:" + fileinfo.LastWriteTime +"<br/>");
Response.Write("所在文件夹:" + fileinfo.DirectoryName +"<br/>");
Response.Write("文件属性:" + fileinfo.Attributes +"<br/>");
Response.Write("<br/>");
}
}
if(Directory.Exists(Server.MapPath("a")))
{
//所有子文件夹
foreach(string item in Directory.GetDirectories(Server.MapPath("a")))
{
Response.Write("<b>文件夹:" + item + "</b><br/>");
DirectoryInfo directoryinfo = new DirectoryInfo(item);
Response.Write("名称:" + directoryinfo.Name + "<br/>");
Response.Write("路径:" + directoryinfo.FullName + "<br/>");
Response.Write("创建时间:" + directoryinfo.CreationTime + "<br/>");
Response.Write("上次访问时间:" + directoryinfo.LastAccessTime + "<br/>");
Response.Write("上次修改时间:" + directoryinfo.LastWriteTime + "<br/>");
Response.Write("父文件夹:" + directoryinfo.Parent + "<br/>");
Response.Write("所在根目录:" + directoryinfo.Root + "<br/>");
Response.Write("<br/>");
}
//所有子文件
foreach (string item in Directory.GetFiles(Server.MapPath("a")))
{
Response.Write("<b>文件:" + item + "</b><br/>");
FileInfo fileinfo = new FileInfo(item);
Response.Write("名称:" + fileinfo.Name + "<br/>");
Response.Write("扩展名:" + fileinfo.Extension +"<br/>");
Response.Write("路径:" + fileinfo.FullName +"<br/>");
Response.Write("大小:" + fileinfo.Length +"<br/>");
Response.Write("创建时间:" + fileinfo.CreationTime +"<br/>");
Response.Write("上次访问时间:" + fileinfo.LastAccessTime +"<br/>");
Response.Write("上次修改时间:" + fileinfo.LastWriteTime +"<br/>");
Response.Write("所在文件夹:" + fileinfo.DirectoryName +"<br/>");
Response.Write("文件属性:" + fileinfo.Attributes +"<br/>");
Response.Write("<br/>");
}
}
展开全部
一定要做成递归函数。
ArrayList list = new ArrayList();
private void GetFileName(string path)
{
try
{
if (!Directory.Exists(path))
{
return;
}
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fi = di.GetFiles();
foreach (FileInfo fiTemp in fi)
{
list.Add(fiTemp.Name);
System.Diagnostics.Debug.WriteLine(fiTemp.Name);
}
DirectoryInfo[] subDirList = di.GetDirectories();
if (subDirList.Length > 0 )
{
for (int i = 0; i < subDirList.Length; i++)
{
GetFileName(subDirList[i].FullName);
}
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
}
ArrayList list = new ArrayList();
private void GetFileName(string path)
{
try
{
if (!Directory.Exists(path))
{
return;
}
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fi = di.GetFiles();
foreach (FileInfo fiTemp in fi)
{
list.Add(fiTemp.Name);
System.Diagnostics.Debug.WriteLine(fiTemp.Name);
}
DirectoryInfo[] subDirList = di.GetDirectories();
if (subDirList.Length > 0 )
{
for (int i = 0; i < subDirList.Length; i++)
{
GetFileName(subDirList[i].FullName);
}
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
private List<string> GetFiles(string path)
{
List<string> dir = Directory.GetDirectories(path).ToList();
List<string> files = Directory.GetFiles(path).ToList();
List<string> fileNames = new List<string>();
foreach (string file in files)
{
fileNames.Add(Path.GetFileName(file));
}
foreach (string item in dir)
{
List<string> subFileNames = GetFiles(item);
fileNames.AddRange(subFileNames);
}
return fileNames;
}
{
List<string> dir = Directory.GetDirectories(path).ToList();
List<string> files = Directory.GetFiles(path).ToList();
List<string> fileNames = new List<string>();
foreach (string file in files)
{
fileNames.Add(Path.GetFileName(file));
}
foreach (string item in dir)
{
List<string> subFileNames = GetFiles(item);
fileNames.AddRange(subFileNames);
}
return fileNames;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
System.IO.Directory.GetFiles("d:\\abc", "*.*", System.IO.SearchOption.AllDirectories);
最后一个参数的意思是找该文件夹下所有的文件(包含子文件夹)
最后一个参数的意思是找该文件夹下所有的文件(包含子文件夹)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询