//1.---------文件夹创建、移动、删除---------
//创建文件夹
Directory.CreateDirectory(Server.MapPath("a"));
Directory.CreateDirectory(Server.MapPath("b"));
Directory.CreateDirectory(Server.MapPath("c"));
//移动b到a
Directory.Move(Server.MapPath("b"), Server.MapPath("ab"));
//删除c
Directory.Delete(Server.MapPath("c"));
//2.---------文件创建、复制、移动、删除---------
//创建文件
//使用File.Create创建再复制/移动/删除时会提示:文件正由另一进程使用,因此该进程无法访问该文件
//改用 FileStream 获取 File.Create 返回的 System.IO.FileStream 再进行关闭就无此问题
FileStream fs;
fs = File.Create(Server.MapPath("a.txt"));
fs.Close();
fs = File.Create(Server.MapPath("b.txt"));
fs.Close();
fs = File.Create(Server.MapPath("c.txt"));
fs.Close();
//复制文件
File.Copy(Server.MapPath("a.txt"), Server.MapPath("aa.txt"));
//移动文件
File.Move(Server.MapPath("b.txt"), Server.MapPath("ab.txt"));
File.Move(Server.MapPath("c.txt"), Server.MapPath("ac.txt"));
//删除文件
File.Delete(Server.MapPath("a.txt"));
//3.---------遍历文件夹中的文件和子文件夹并显示其属性---------
if(Directory.Exists(Server.MapPath("a")))
{
//所有子文件夹
foreach(string item in Directory.GetDirectories(Server.MapPath("a")))
{
Response.Write("文件夹:" + item + " ");
DirectoryInfo directoryinfo = new DirectoryInfo(item);
Response.Write("名称:" + directoryinfo.Name + " ");
Response.Write("路径:" + directoryinfo.FullName + " ");
Response.Write("创建时间:" + directoryinfo.CreationTime + " ");
Response.Write("上次访问时间:" + directoryinfo.LastAccessTime + " ");
Response.Write("上次修改时间:" + directoryinfo.LastWriteTime + " ");
Response.Write("父文件夹:" + directoryinfo.Parent + " ");
Response.Write("所在根目录:" + directoryinfo.Root + " ");
Response.Write(" ");
}
//所有子文件
foreach (string item in Directory.GetFiles(Server.MapPath("a")))
{
Response.Write("文件:" + item + " ");
FileInfo fileinfo = new FileInfo(item);
Response.Write("名称:" + fileinfo.Name + " ");
Response.Write("扩展名:" + fileinfo.Extension +" ");
Response.Write("路径:" + fileinfo.FullName +" ");
Response.Write("大小:" + fileinfo.Length +" ");
Response.Write("创建时间:" + fileinfo.CreationTime +" ");
Response.Write("上次访问时间:" + fileinfo.LastAccessTime +" ");
Response.Write("上次修改时间:" + fileinfo.LastWriteTime +" ");
Response.Write("所在文件夹:" + fileinfo.DirectoryName +" ");
Response.Write("文件属性:" + fileinfo.Attributes +" ");
Response.Write(" ");
}
}
//4.---------文件读写---------
if (File.Exists(Server.MapPath("aa.txt")))
{
StreamWriter streamwrite = new StreamWriter(Server.MapPath("aa.txt"));
streamwrite.WriteLine("木子屋");
streamwrite.WriteLine("http://www.111com.net/");
streamwrite.Write("2008-04-13");
streamwrite.Close();
StreamReader streamreader = new StreamReader(Server.MapPath("aa.txt"));
Response.Write(streamreader.ReadLine());
Response.Write(streamreader.ReadToEnd());
streamreader.Close();
}
|