C#對于文件操作

作者: 李超  來源: 博客園  發布時間: 2008-09-09 19:03  閱讀: 23793 次  推薦: 2   原文鏈接   [收藏]  
 
[1] C#對于文件操作
[2] C#對于文件操作

 

Code
//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); //調用函數刪除文件夾 
} 

  // ======================================================
  // 實現一個靜態方法將指定文件夾下面的所有內容copy到目標文件夾下面
  // 如果目標文件夾為只讀屬性就會報錯。
  // April 18April2005 In STU
  // ======================================================

 

 

Code

public static void CopyDir(string srcPath,string aimPath)
{
    try
    {
        // 檢查目標目錄是否以目錄分割字符結束如果不是則添加之
        if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar) 
            aimPath += Path.DirectorySeparatorChar;
        // 判斷目標目錄是否存在如果不存在則新建之
        if(!Directory.Exists(aimPath)) 
            Directory.CreateDirectory(aimPath);
        // 得到源目錄的文件列表,該里面是包含文件以及目錄路徑的一個數組
        // 如果你指向copy目標文件下面的文件而不包含目錄請使用下面的方法
        // string[] fileList = Directory.GetFiles(srcPath);
        string[] fileList = Directory.GetFileSystemEntries(srcPath);
        // 遍歷所有的文件和目錄
        foreach(string file in fileList)
        {
            // 先當作目錄處理如果存在這個目錄就遞歸Copy該目錄下面的文件
            if(Directory.Exists(file))
                CopyDir(file,aimPath+Path.GetFileName(file));
            // 否則直接Copy文件
            else
                File.Copy(file,aimPath+Path.GetFileName(file),true);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show (e.ToString());
    }
}
[第1頁][第2頁]
2
0
 
標簽:C# 文件操作
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()