我愛我的家園!

          成功在于你是否努力,希望在于你是否相信自己!

           

          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:\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
          ? // ======================================================
          ? 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());
          ?? }
          ? }

          ? // ======================================================
          ? // 實現一個靜態方法將指定文件夾下面的所有內容Detele
          ? // 測試的時候要小心操作,刪除之后無法恢復。
          ? // April 18April2005 In STU
          ? // ======================================================
          ? public static void DeleteDir(string aimPath)
          ? {
          ?? try
          ?? {
          ??? // 檢查目標目錄是否以目錄分割字符結束如果不是則添加之
          ??? if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)
          ???? aimPath += Path.DirectorySeparatorChar;
          ??? // 得到源目錄的文件列表,該里面是包含文件以及目錄路徑的一個數組
          ??? // 如果你指向Delete目標文件下面的文件而不包含目錄請使用下面的方法
          ??? // string[] fileList = Directory.GetFiles(aimPath);
          ??? string[] fileList = Directory.GetFileSystemEntries(aimPath);
          ??? // 遍歷所有的文件和目錄
          ??? foreach(string file in fileList)
          ??? {
          ???? // 先當作目錄處理如果存在這個目錄就遞歸Delete該目錄下面的文件
          ???? if(Directory.Exists(file))
          ???? {
          ????? DeleteDir(aimPath+Path.GetFileName(file));
          ???? }
          ????? // 否則直接Delete文件
          ???? else
          ???? {
          ????? File.Delete (aimPath+Path.GetFileName(file));
          ???? }
          ??? }
          ??? //刪除文件夾
          ??? System.IO .Directory .Delete (aimPath,true);
          ?? }
          ?? catch (Exception e)
          ?? {
          ??? MessageBox.Show (e.ToString());
          ?? }
          ? }
          ?

          ?

          需要引用命名空間:
          using System.IO;

          /**//// <summary>
          ? /// 拷貝文件夾(包括子文件夾)到指定文件夾下,源文件夾和目標文件夾均需絕對路徑. 格式: CopyFolder(源文件夾,目標文件夾);
          ? /// </summary>
          ? /// <param name=”strFromPath”></param>
          ? /// <param name=”strToPath”></param>

          ? //————————————————–
          ? //作者:明天去要飯? QQ:305725744
          ?//—————————————————

          ? public static void CopyFolder(string strFromPath,string strToPath)
          ? {
          ?? //如果源文件夾不存在,則創建
          ?? if (!Directory.Exists(strFromPath))
          ?? {???
          ??? Directory.CreateDirectory(strFromPath);
          ?? }??

          ?? //取得要拷貝的文件夾名
          ?? string strFolderName = strFromPath.Substring(strFromPath.LastIndexOf(”\\”) + 1,strFromPath.Length - strFromPath.LastIndexOf(”\\”) - 1);??

          ?? //如果目標文件夾中沒有源文件夾則在目標文件夾中創建源文件夾
          ?? if (!Directory.Exists(strToPath + “\\” + strFolderName))
          ?? {???
          ??? Directory.CreateDirectory(strToPath + “\\” + strFolderName);
          ?? }
          ?? //創建數組保存源文件夾下的文件名
          ?? string[] strFiles = Directory.GetFiles(strFromPath);

          ?? //循環拷貝文件
          ?? for(int i = 0;i < strFiles.Length;i++)
          ?? {
          ??? //取得拷貝的文件名,只取文件名,地址截掉。
          ??? string strFileName = strFiles[i].Substring(strFiles[i].LastIndexOf(”\\”) + 1,strFiles[i].Length - strFiles[i].LastIndexOf(”\\”) - 1);
          ??? //開始拷貝文件,true表示覆蓋同名文件
          ??? File.Copy(strFiles[i],strToPath + “\\” + strFolderName + “\\” + strFileName,true);
          ?? }
          ?
          ?? //創建DirectoryInfo實例
          ?? DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);
          ?? //取得源文件夾下的所有子文件夾名稱
          ?? DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
          ?? for (int j = 0;j < ZiPath.Length;j++)
          ?? {
          ??? //獲取所有子文件夾名
          ??? string strZiPath = strFromPath + “\\” + ZiPath[j].ToString();??
          ??? //把得到的子文件夾當成新的源文件夾,從頭開始新一輪的拷貝
          ??? CopyFolder(strZiPath,strToPath + “\\” + strFolderName);
          ?? }
          ? }

          posted on 2008-09-04 14:24 死神 閱讀(381) 評論(0)  編輯  收藏 所屬分類: C#學習

          導航

          統計

          公告

          歡迎大家來到我的個人世界!

          常用鏈接

          留言簿(3)

          隨筆分類(5)

          隨筆檔案(9)

          文章分類(37)

          文章檔案(41)

          相冊

          語音技術

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 凤阳县| 溆浦县| 凤山市| 林周县| 汉沽区| 威远县| 开鲁县| 林甸县| 岱山县| 丰顺县| 祁东县| 长海县| 连南| 沙洋县| 宜丰县| 昌图县| 囊谦县| 涡阳县| 柯坪县| 永修县| 邯郸市| 桂平市| 建昌县| 桐梓县| 南投县| 大安市| 宿迁市| 连平县| 江安县| 黄陵县| 镇巴县| 伊吾县| 庄浪县| 三穗县| 云阳县| 昆山市| 锡林郭勒盟| 巴塘县| 安义县| 黑河市| 马龙县|