記錄學C#的點滴 文章多為轉載 留作備查之用
C#中常用的經典文件操作方法
C#追加文件
StreamWriter sw = File.AppendText(Server.MapPath(".")+"\myText.txt");
sw.WriteLine("追逐理想");
sw.WriteLine("kzlll");
sw.WriteLine(".NET筆記");
sw.Flush();
sw.Close();
C#拷貝文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"\myText.txt";
NewFile = Server.MapPath(".")+"\myTextCopy.txt";
File.Copy(OrignFile,NewFile,true);
C#刪除文件
string delFile = Server.MapPath(".")+"\myTextCopy.txt";
File.Delete(delFile);
C#移動文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"\myText.txt";
NewFile = Server.MapPath(".")+"\myTextCopy.txt";
File.Move(OrignFile,NewFile);
C#創建目錄
// 創建目錄c:sixAge
DirectoryInfo d=Directory.CreateDirectory("c:\sixAge");
// d1指向c:sixAgesixAge1
DirectoryInfo d1=d.CreateSubdirectory("sixAge1");
// d2指向c:sixAgesixAge1sixAge1_1
DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");
// 將當前目錄設為c:sixAge
Directory.SetCurrentDirectory("c:\sixAge");
// 創建目錄c:sixAgesixAge2
Directory.CreateDirectory("sixAge2");
// 創建目錄c:sixAgesixAge2sixAge2_1
Directory.CreateDirectory("sixAge2\sixAge2_1");
遞歸刪除文件夾及文件
<%@ Page Language=C#%>
<%@ Import namespace="System.IO"%>
<Script runat=server>