C#常用的檔案操作

來源:互聯網
上載者:User

 

C#常用的檔案操作 
C#寫入/讀出文字檔 
public void Page_Load(Object src,EventArgs e)  
{  
   StreamWriter rw = File.CreateText(Server.MapPath(".")+"\\myText.txt");  
   rw.WriteLine("追逐理想");  
   rw.WriteLine("kzlll");  
   rw.WriteLine(".NET筆記");  
   rw.Flush();  
   rw.Close();  

開啟文字檔 
StreamReader sr = File.OpenText(Server.MapPath(".")+"\\myText.txt");  
StringBuilder output = new StringBuilder();  
string rl;  
while((rl=sr.ReadLine())!=null)  
{  
output.Append(rl+"<br>");  
}  
lblFile.Text = output.ToString();  
sr.Close();  
  
 
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:\sixAge\sixAge1  
          DirectoryInfo d1=d.CreateSubdirectory("sixAge1");  
          // d2指向c:\sixAge\sixAge1\sixAge1_1  
          DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");  
          // 將目前的目錄設為c:\sixAge  
          Directory.SetCurrentDirectory("c:\\sixAge");  
          // 建立目錄c:\sixAge\sixAge2  
          Directory.CreateDirectory("sixAge2");  
          // 建立目錄c:\sixAge\sixAge2\sixAge2_1  
          Directory.CreateDirectory("sixAge2\\sixAge2_1");  
 
遞迴刪除檔案夾及檔案 
<%@ Page Language=C#%>  
<%@ Import namespace="System.IO"%>  
<Script runat=server>  
public void DeleteFolder(string dir)  
{  
     if (Directory.Exists(dir))   //如果存在這個檔案夾刪除之  
       {  
           foreach(string d in Directory.GetFileSystemEntries(dir))  
              {  
                 if(File.Exists(d))  
                 File.Delete(d);    //直接刪除其中的檔案  
                 else  
                 DeleteFolder(d);       //遞迴刪除子檔案夾  
               }  
            Directory.Delete(dir);    //刪除已空檔案夾  
            Response.Write(dir+"  檔案夾刪除成功");  
        }  
     else  
        Response.Write(dir+"  該檔案夾不存在");  //如果檔案夾不存在則提示  
}  
protected void Page_Load (Object sender ,EventArgs e)  
{  
string Dir="D:\\gbook\\11";  
DeleteFolder(Dir);           //調用函數刪除檔案夾  
}  
</Script>  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.