C#停止文件讀寫、創立、復制、挪動、刪除的辦法。本站提示廣大學習愛好者:(C#停止文件讀寫、創立、復制、挪動、刪除的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#停止文件讀寫、創立、復制、挪動、刪除的辦法正文
本文實例講述了C#停止文件讀寫、創立、復制、挪動、刪除的辦法。分享給年夜家供年夜家參考。詳細剖析以下:
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("a//b")); //刪除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("a//a.txt")); //挪動文件 File.Move(Server.MapPath("b.txt"),Server.MapPath("a//b.txt")); File.Move(Server.MapPath("c.txt"),Server.MapPath("a//c.txt")); //刪除文件 File.Delete(Server.MapPath("a.txt"));
3.遍歷文件夾中的文件和子文件夾並顯示其屬性
if(Directory.Exists(Server.MapPath("a"))) { //一切子文件夾 foreach(string item inDirectory.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 inDirectory.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/>"); } }
4.文件讀寫
if (File.Exists(Server.MapPath("a//a.txt"))) { StreamWriter streamwrite = newStreamWriter(Server.MapPath("a//a.txt")); streamwrite.WriteLine("木子屋"); streamwrite.WriteLine("http://www.jb51.net/"); streamwrite.Write("2008-04-13"); streamwrite.Close(); StreamReader streamreader = newStreamReader(Server.MapPath("a//a.txt")); Response.Write(streamreader.ReadLine()); Response.Write(streamreader.ReadToEnd()); streamreader.Close(); }
願望本文所述對年夜家的C#法式設計有所贊助。