ASP.NET----实现文件夹信息

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace NET.MST.Fifth.UseDirectoryInfo
{
class UseDirectoryInfo
{
static void Main(string[] args)
{
ProcessDirectory();
Console.Read();
}
//操作文件夹的功能
static void ProcessDirectory()
{
DirectoryInfo di
= null;
try
{
//创建文件夹
di = new DirectoryInfo("C:\\Test");
di.Create();
//为文件夹添加子文件夹
di.CreateSubdirectory("subdic");
//为子文件夹添加一个文件
FileInfo info = new FileInfo("C:\\Test\\subdic\\text.txt");
using (FileStream fs = info.Create()) { }
//读取文件夹信息
ReadDirectoryInfo(di);
//移动文件夹
di.MoveTo("C:\\Test2");
}
finally
{
if (di != null)
//删除文件夹
di.Delete(true); //一起删除子文件和子文件夹
}
}
//读取文件夹状态信息的功能
static void ReadDirectoryInfo(DirectoryInfo di)
{
Console.WriteLine(
"文件夹名为:" + di.Name);
Console.WriteLine(
"父文件夹为:" + di.Parent.Name);
Console.WriteLine(
"文件夹创建于:" + di.CreationTime);
Console.WriteLine(
"文件夹最后修改时间:" + di.LastWriteTime);
Console.WriteLine(
"文件夹最后访问时间:" + di.LastAccessTime);
Console.WriteLine(
"文件夹全名是:" + di.FullName);
Console.WriteLine(
"文件夹中的文件:");
FileInfo[] fis
= di.GetFiles();
foreach (FileInfo fi in fis)
ReadFileInfo(fi);
Console.WriteLine(
"文件夹中的子文件夹:");
DirectoryInfo[] dis
= di.GetDirectories();
foreach (DirectoryInfo sdi in dis)
ReadDirectoryInfo(sdi);
}
//读取文件状态信息的功能
static void ReadFileInfo(FileInfo fi)
{
Console.WriteLine(
"文件名为:" + fi.Name);
Console.WriteLine(
"文件在:" + fi.DirectoryName);
Console.WriteLine(
"文件的长度: " + fi.Length);
Console.WriteLine(
"文件创建于:" + fi.CreationTime);
Console.WriteLine(
"文件最后修改时间:" + fi.LastWriteTime);
Console.WriteLine(
"文件最后访问时间:" + fi.LastAccessTime);
Console.WriteLine(
"文件全名是:" + fi.FullName);
Console.WriteLine(
"文件扩展名是:" + fi.Extension);
}
}
}

作者: brainmao   发布时间: 2011-06-15