{"id":694,"date":"2009-10-27T21:19:00","date_gmt":"2009-10-27T01:19:00","guid":{"rendered":""},"modified":"2013-11-17T12:42:13","modified_gmt":"2013-11-17T04:42:13","slug":"c%e4%b8%ad%e5%b8%b8%e7%94%a8%e7%9a%84%e7%bb%8f%e5%85%b8%e6%96%87%e4%bb%b6%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/694.html","title":{"rendered":"C#\u4e2d\u5e38\u7528\u7684\u7ecf\u5178\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5"},"content":{"rendered":"<p>C#\u8ffd\u52a0\u6587\u4ef6 <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nStreamWriter sw = File.AppendText(Server.MapPath(&quot;.&quot;)+&quot;\\\\myText.txt&quot;); \r\nsw.WriteLine(&quot;\u8ffd\u9010\u7406\u60f3&quot;); \r\nsw.WriteLine(&quot;kzlll&quot;); \r\nsw.WriteLine(&quot;.NET\u7b14\u8bb0&quot;); \r\nsw.Flush(); \r\nsw.Close();\r\n<\/pre>\n<p>C#\u62f7\u8d1d\u6587\u4ef6 <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstring OrignFile,NewFile; \r\nOrignFile = Server.MapPath(&quot;.&quot;)+&quot;\\\\myText.txt&quot;; \r\nNewFile = Server.MapPath(&quot;.&quot;)+&quot;\\\\myTextCopy.txt&quot;; \r\nFile.Copy(OrignFile,NewFile,true);\r\n<\/pre>\n<p>C#\u5220\u9664\u6587\u4ef6 <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstring delFile = Server.MapPath(&quot;.&quot;)+&quot;\\\\myTextCopy.txt&quot;; \r\nFile.Delete(delFile);\r\n<\/pre>\n<p>C#\u79fb\u52a8\u6587\u4ef6 <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstring OrignFile,NewFile; \r\nOrignFile = Server.MapPath(&quot;.&quot;)+&quot;\\\\myText.txt&quot;; \r\nNewFile = Server.MapPath(&quot;.&quot;)+&quot;\\\\myTextCopy.txt&quot;; \r\nFile.Move(OrignFile,NewFile);\r\n<\/pre>\n<p>C#\u521b\u5efa\u76ee\u5f55 <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ \u521b\u5efa\u76ee\u5f55c:\\sixAge \r\nDirectoryInfo d=Directory.CreateDirectory(&quot;c:\\\\sixAge&quot;); \r\n\/\/ d1\u6307\u5411c:\\sixAge\\sixAge1 \r\nDirectoryInfo d1=d.CreateSubdirectory(&quot;sixAge1&quot;); \r\n\/\/ d2\u6307\u5411c:\\sixAge\\sixAge1\\sixAge1_1 \r\nDirectoryInfo d2=d1.CreateSubdirectory(&quot;sixAge1_1&quot;); \r\n\/\/ \u5c06\u5f53\u524d\u76ee\u5f55\u8bbe\u4e3ac:\\sixAge \r\nDirectory.SetCurrentDirectory(&quot;c:\\\\sixAge&quot;); \r\n\/\/ \u521b\u5efa\u76ee\u5f55c:\\sixAge\\sixAge2 \r\nDirectory.CreateDirectory(&quot;sixAge2&quot;); \r\n\/\/ \u521b\u5efa\u76ee\u5f55c:\\sixAge\\sixAge2\\sixAge2_1 \r\nDirectory.CreateDirectory(&quot;sixAge2\\\\sixAge2_1&quot;);\r\n<\/pre>\n<p>\u9012\u5f52\u5220\u9664\u6587\u4ef6\u5939\u53ca\u6587\u4ef6 <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic void DeleteFolder(string dir) \r\n{ \r\n    if (Directory.Exists(dir)) \/\/\u5982\u679c\u5b58\u5728\u8fd9\u4e2a\u6587\u4ef6\u5939\u5220\u9664\u4e4b \r\n    { \r\n        foreach(string d in Directory.GetFileSystemEntries(dir)) \r\n        { \r\n            if(File.Exists(d)) \r\n                File.Delete(d); \/\/\u76f4\u63a5\u5220\u9664\u5176\u4e2d\u7684\u6587\u4ef6 \r\n            else \r\n                DeleteFolder(d); \/\/\u9012\u5f52\u5220\u9664\u5b50\u6587\u4ef6\u5939 \r\n        } \r\n        Directory.Delete(dir); \/\/\u5220\u9664\u5df2\u7a7a\u6587\u4ef6\u5939 \r\n        Response.Write(dir+&quot; \u6587\u4ef6\u5939\u5220\u9664\u6210\u529f&quot;); \r\n    } \r\n    else \r\n        Response.Write(dir+&quot; \u8be5\u6587\u4ef6\u5939\u4e0d\u5b58\u5728&quot;); \/\/\u5982\u679c\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\u5219\u63d0\u793a \r\n}\r\n\r\nprotected void Page_Load (Object sender ,EventArgs e) \r\n{ \r\n    string Dir=&quot;D:\\\\gbook\\\\11&quot;; \r\n    DeleteFolder(Dir); \/\/\u8c03\u7528\u51fd\u6570\u5220\u9664\u6587\u4ef6\u5939 \r\n}\r\n\r\n\/\/ ======================================================\r\n\/\/ \u5b9e\u73b0\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\u5c06\u6307\u5b9a\u6587\u4ef6\u5939\u4e0b\u9762\u7684\u6240\u6709\u5185\u5bb9copy\u5230\u76ee\u6807\u6587\u4ef6\u5939\u4e0b\u9762\r\n\/\/ \u5982\u679c\u76ee\u6807\u6587\u4ef6\u5939\u4e3a\u53ea\u8bfb\u5c5e\u6027\u5c31\u4f1a\u62a5\u9519\u3002\r\n\/\/ April 18April2005 In STU\r\n\/\/ ======================================================\r\npublic static void CopyDir(string srcPath,string aimPath)\r\n{\r\n   try\r\n   {\r\n    \/\/ \u68c0\u67e5\u76ee\u6807\u76ee\u5f55\u662f\u5426\u4ee5\u76ee\u5f55\u5206\u5272\u5b57\u7b26\u7ed3\u675f\u5982\u679c\u4e0d\u662f\u5219\u6dfb\u52a0\u4e4b\r\n    if(aimPath&#x5B;aimPath.Length-1] != Path.DirectorySeparatorChar) \r\n     aimPath += Path.DirectorySeparatorChar;\r\n    \/\/ \u5224\u65ad\u76ee\u6807\u76ee\u5f55\u662f\u5426\u5b58\u5728\u5982\u679c\u4e0d\u5b58\u5728\u5219\u65b0\u5efa\u4e4b\r\n    if(!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);\r\n    \/\/ \u5f97\u5230\u6e90\u76ee\u5f55\u7684\u6587\u4ef6\u5217\u8868\uff0c\u8be5\u91cc\u9762\u662f\u5305\u542b\u6587\u4ef6\u4ee5\u53ca\u76ee\u5f55\u8def\u5f84\u7684\u4e00\u4e2a\u6570\u7ec4\r\n    \/\/ \u5982\u679c\u4f60\u6307\u5411copy\u76ee\u6807\u6587\u4ef6\u4e0b\u9762\u7684\u6587\u4ef6\u800c\u4e0d\u5305\u542b\u76ee\u5f55\u8bf7\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\r\n    \/\/ string&#x5B;] fileList = Directory.GetFiles(srcPath);\r\n    string&#x5B;] fileList = Directory.GetFileSystemEntries(srcPath);\r\n    \/\/ \u904d\u5386\u6240\u6709\u7684\u6587\u4ef6\u548c\u76ee\u5f55\r\n    foreach(string file in fileList)\r\n    {\r\n     \/\/ \u5148\u5f53\u4f5c\u76ee\u5f55\u5904\u7406\u5982\u679c\u5b58\u5728\u8fd9\u4e2a\u76ee\u5f55\u5c31\u9012\u5f52Copy\u8be5\u76ee\u5f55\u4e0b\u9762\u7684\u6587\u4ef6\r\n     if(Directory.Exists(file))\r\n      CopyDir(file,aimPath+Path.GetFileName(file));\r\n      \/\/ \u5426\u5219\u76f4\u63a5Copy\u6587\u4ef6\r\n     else\r\n      File.Copy(file,aimPath+Path.GetFileName(file),true);\r\n    }\r\n   }\r\n   catch (Exception e)\r\n   {\r\n    MessageBox.Show (e.ToString());\r\n   }\r\n}\r\n\r\n\/\/ ======================================================\r\n\/\/ \u5b9e\u73b0\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\u5c06\u6307\u5b9a\u6587\u4ef6\u5939\u4e0b\u9762\u7684\u6240\u6709\u5185\u5bb9Detele\r\n\/\/ \u6d4b\u8bd5\u7684\u65f6\u5019\u8981\u5c0f\u5fc3\u64cd\u4f5c\uff0c\u5220\u9664\u4e4b\u540e\u65e0\u6cd5\u6062\u590d\u3002\r\n\/\/ April 18April2005 In STU\r\n\/\/ ======================================================\r\npublic static void DeleteDir(string aimPath)\r\n{\r\n   try\r\n   {\r\n    \/\/ \u68c0\u67e5\u76ee\u6807\u76ee\u5f55\u662f\u5426\u4ee5\u76ee\u5f55\u5206\u5272\u5b57\u7b26\u7ed3\u675f\u5982\u679c\u4e0d\u662f\u5219\u6dfb\u52a0\u4e4b\r\n    if(aimPath&#x5B;aimPath.Length-1] != Path.DirectorySeparatorChar) \r\n     aimPath += Path.DirectorySeparatorChar;\r\n    \/\/ \u5f97\u5230\u6e90\u76ee\u5f55\u7684\u6587\u4ef6\u5217\u8868\uff0c\u8be5\u91cc\u9762\u662f\u5305\u542b\u6587\u4ef6\u4ee5\u53ca\u76ee\u5f55\u8def\u5f84\u7684\u4e00\u4e2a\u6570\u7ec4\r\n    \/\/ \u5982\u679c\u4f60\u6307\u5411Delete\u76ee\u6807\u6587\u4ef6\u4e0b\u9762\u7684\u6587\u4ef6\u800c\u4e0d\u5305\u542b\u76ee\u5f55\u8bf7\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\r\n    \/\/ string&#x5B;] fileList = Directory.GetFiles(aimPath);\r\n    string&#x5B;] fileList = Directory.GetFileSystemEntries(aimPath);\r\n    \/\/ \u904d\u5386\u6240\u6709\u7684\u6587\u4ef6\u548c\u76ee\u5f55\r\n    foreach(string file in fileList)\r\n    {\r\n     \/\/ \u5148\u5f53\u4f5c\u76ee\u5f55\u5904\u7406\u5982\u679c\u5b58\u5728\u8fd9\u4e2a\u76ee\u5f55\u5c31\u9012\u5f52Delete\u8be5\u76ee\u5f55\u4e0b\u9762\u7684\u6587\u4ef6\r\n     if(Directory.Exists(file))\r\n     {\r\n      DeleteDir(aimPath+Path.GetFileName(file));\r\n     }\r\n      \/\/ \u5426\u5219\u76f4\u63a5Delete\u6587\u4ef6\r\n     else\r\n     {\r\n      File.Delete (aimPath+Path.GetFileName(file));\r\n     }\r\n    }\r\n    \/\/\u5220\u9664\u6587\u4ef6\u5939\r\n    System.IO .Directory .Delete (aimPath,true);\r\n   }\r\n   catch (Exception e)\r\n   {\r\n    MessageBox.Show (e.ToString());\r\n   }\r\n}\r\n<\/pre>\n<p>\u9700\u8981\u5f15\u7528\u547d\u540d\u7a7a\u95f4\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System.IO;\r\n\r\npublic static void CopyFolder(string strFromPath,string strToPath)\r\n{\r\n   \/\/\u5982\u679c\u6e90\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\r\n   if (!Directory.Exists(strFromPath))\r\n   {    \r\n    Directory.CreateDirectory(strFromPath);\r\n   }  \r\n\r\n   \/\/\u53d6\u5f97\u8981\u62f7\u8d1d\u7684\u6587\u4ef6\u5939\u540d\r\n   string strFolderName = strFromPath.Substring(strFromPath.LastIndexOf(&quot;\\\\&quot;) + 1,strFromPath.Length - strFromPath.LastIndexOf(&quot;\\\\&quot;) - 1);  \r\n\r\n   \/\/\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5939\u4e2d\u6ca1\u6709\u6e90\u6587\u4ef6\u5939\u5219\u5728\u76ee\u6807\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u6e90\u6587\u4ef6\u5939\r\n   if (!Directory.Exists(strToPath + &quot;\\\\&quot; + strFolderName))\r\n   {    \r\n    Directory.CreateDirectory(strToPath + &quot;\\\\&quot; + strFolderName);\r\n   }\r\n   \/\/\u521b\u5efa\u6570\u7ec4\u4fdd\u5b58\u6e90\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\u540d\r\n   string&#x5B;] strFiles = Directory.GetFiles(strFromPath);\r\n\r\n   \/\/\u5faa\u73af\u62f7\u8d1d\u6587\u4ef6\r\n   for(int i = 0;i &lt; strFiles.Length;i++)\r\n   {\r\n    \/\/\u53d6\u5f97\u62f7\u8d1d\u7684\u6587\u4ef6\u540d\uff0c\u53ea\u53d6\u6587\u4ef6\u540d\uff0c\u5730\u5740\u622a\u6389\u3002\r\n    string strFileName = strFiles&#x5B;i].Substring(strFiles&#x5B;i].LastIndexOf(&quot;\\\\&quot;) + 1,strFiles&#x5B;i].Length - strFiles&#x5B;i].LastIndexOf(&quot;\\\\&quot;) - 1);\r\n    \/\/\u5f00\u59cb\u62f7\u8d1d\u6587\u4ef6,true\u8868\u793a\u8986\u76d6\u540c\u540d\u6587\u4ef6\r\n    File.Copy(strFiles&#x5B;i],strToPath + &quot;\\\\&quot; + strFolderName + &quot;\\\\&quot; + strFileName,true);\r\n   }\r\n\r\n   \/\/\u521b\u5efaDirectoryInfo\u5b9e\u4f8b\r\n   DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);\r\n   \/\/\u53d6\u5f97\u6e90\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709\u5b50\u6587\u4ef6\u5939\u540d\u79f0\r\n   DirectoryInfo&#x5B;] ZiPath = dirInfo.GetDirectories();\r\n   for (int j = 0;j &lt; ZiPath.Length;j++)\r\n   {\r\n    \/\/\u83b7\u53d6\u6240\u6709\u5b50\u6587\u4ef6\u5939\u540d\r\n    string strZiPath = strFromPath + &quot;\\\\&quot; + ZiPath&#x5B;j].ToString();   \r\n    \/\/\u628a\u5f97\u5230\u7684\u5b50\u6587\u4ef6\u5939\u5f53\u6210\u65b0\u7684\u6e90\u6587\u4ef6\u5939\uff0c\u4ece\u5934\u5f00\u59cb\u65b0\u4e00\u8f6e\u7684\u62f7\u8d1d\r\n    CopyFolder(strZiPath,strToPath + &quot;\\\\&quot; + strFolderName);\r\n   }\r\n}\r\n<\/pre>\n<p>CreateDirectory\u65b9\u6cd5\u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u6cd5<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing   System;   \r\nusing   System.IO;   \r\n    \r\nclass   Test     \r\n{   \r\n          public   static   void   Main()     \r\n          {   \r\n                  \/\/   Specify   the   directory   you   want   to   manipulate.   \r\n                  string   path   =   @&quot;c:\\MyDir&quot;;   \r\n    \r\n                  try     \r\n                  {   \r\n                          \/\/   Determine   whether   the   directory   exists.   \r\n                          if   (Directory.Exists(path))     \r\n                          {   \r\n                                  Console.WriteLine(&quot;That   path   exists   already.&quot;);   \r\n                                  return;   \r\n                          }   \r\n    \r\n                          \/\/   Try   to   create   the   directory.   \r\n                          DirectoryInfo   di   =   Directory.CreateDirectory(path);   \r\n                          Console.WriteLine(&quot;The   directory   was   created   successfully   at   {0}.&quot;,   Directory.GetCreationTime(path));   \r\n    \r\n                          \/\/   Delete   the   directory.   \r\n                          di.Delete();   \r\n                          Console.WriteLine(&quot;The   directory   was   deleted   successfully.&quot;);   \r\n                  }     \r\n                  catch   (Exception   e)     \r\n                  {   \r\n                          Console.WriteLine(&quot;The   process   failed:   {0}&quot;,   e.ToString());   \r\n                  }     \r\n                  finally   {}   \r\n          }   \r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C#\u8ffd\u52a0\u6587\u4ef6 StreamWriter sw = File.AppendText(Server.MapPath [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-694","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=694"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/694\/revisions"}],"predecessor-version":[{"id":4594,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/694\/revisions\/4594"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}