本文具體給出了C#常用的文件操作方法,包括C#追加文件,C#拷貝文件,C#刪除文件,C#移動(dòng)文件,C#創(chuàng)建目錄。
(1)C#追加文件
StreamWriter sw = File.AppendText(Server.MapPath(".")+"//myText.txt");
sw.WriteLine("武林網(wǎng)VEVB");
sw.WriteLine("提供最精彩的IT文章");
sw.WriteLine("武林網(wǎng)VEVB,大家的樂園");
sw.Flush();
sw.Close();
(2)C#拷貝文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"//myText.txt";
NewFile = Server.MapPath(".")+"//myTextCopy.txt";
File.Copy(OrignFile,NewFile,true);
(3)C#刪除文件
string delFile = Server.MapPath(".")+"//myTextCopy.txt";
File.Delete(delFile);
(4)C#移動(dòng)文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"//myText.txt";
NewFile = Server.MapPath(".")+"//myTextCopy.txt";
File.Move(OrignFile,NewFile);
(5)C#創(chuàng)建目錄
// 創(chuàng)建目錄c:/sixAge
DirectoryInfo d=Directory.CreateDirectory("c://sixAge");
// d1指向c:/sixAge/sixAge1
DirectoryInfo d1=d.CreateSubdirectory("sixAge1");
// d2指向c:/sixAge/sixAge1/sixAge1_1
DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");
// 將當(dāng)前目錄設(shè)為c:/sixAge
Directory.SetCurrentDirectory("c://sixAge");
// 創(chuàng)建目錄c:/sixAge/sixAge2
Directory.CreateDirectory("sixAge2");
// 創(chuàng)建目錄c:/sixAge/sixAge2/sixAge2_1
Directory.CreateDirectory("sixAge2//sixAge2_1");
新聞熱點(diǎn)
疑難解答