{"id":542,"date":"2009-07-25T19:37:00","date_gmt":"2009-07-25T11:37:00","guid":{"rendered":""},"modified":"2013-11-17T13:23:22","modified_gmt":"2013-11-17T05:23:22","slug":"c%e5%a4%84%e7%90%86%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/542.html","title":{"rendered":"C#\u5904\u7406\u6587\u672c\u6587\u4ef6"},"content":{"rendered":"<p>C#\u64cd\u4f5c\u6587\u672c\u6587\u4ef6\u7684\u7c7bStreamReader \u53ca StreamWriter \u8fd9\u4e24\u4e2a\u7c7b\u4e3b\u8981\u662f\u7528\u662f\u6587\u672c\u6587\u4ef6\u7684\u64cd\u4f5c\uff0c\u4e0d\u80fd\u7528\u4e8e\u4e8c\u8fdb\u5236\u6587\u4ef6<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n    protected void ReadFile()\r\n    {\r\n        StreamReader sr = File.OpenText(Server.MapPath(&quot;~\/zxf.txt&quot;));\r\n        string txt=&quot;&quot;;\r\n        while ((txt=sr.ReadLine() )!= null)\r\n        {\r\n            TextBox1.Text += txt;\r\n        }\r\n        sr.Close();\r\n        sr.Dispose();\r\n\r\n    }\r\n\r\n    protected void WriteFile()\r\n    {\r\n        StreamWriter sw=new StreamWriter(Server.MapPath(&quot;~\/zxf.txt&quot;));\r\n        sw.WriteLine(TextBox1.Text);\r\n        sw.Close();\r\n        sw.Dispose();\r\n    }\r\n<\/pre>\n<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\n&lt;%@ Page Language=C#%&gt; \r\n&lt;%@ Import namespace=&quot;System.IO&quot;%&gt; \r\n&lt;Script runat=server&gt; \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\/\/ ======================================================\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\n\/**\/\/\/\/ &lt;summary&gt;\r\n\/\/\/ \u62f7\u8d1d\u6587\u4ef6\u5939(\u5305\u62ec\u5b50\u6587\u4ef6\u5939)\u5230\u6307\u5b9a\u6587\u4ef6\u5939\u4e0b,\u6e90\u6587\u4ef6\u5939\u548c\u76ee\u6807\u6587\u4ef6\u5939\u5747\u9700\u7edd\u5bf9\u8def\u5f84. \u683c\u5f0f: CopyFolder(\u6e90\u6587\u4ef6\u5939,\u76ee\u6807\u6587\u4ef6\u5939);\r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;param name=&quot;strFromPath&quot;&gt;&lt;\/param&gt;\r\n\/\/\/ &lt;param name=&quot;strToPath&quot;&gt;&lt;\/param&gt;\r\n\r\n\/\/--------------------------------------------------\r\n\/\/\u4f5c\u8005:\u660e\u5929\u53bb\u8981\u996d QQ:305725744\r\n\/\/---------------------------------------------------\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>\u4e00\uff0e\u8bfb\u53d6\u6587\u672c\u6587\u4ef6<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/**\/\/\/\/ &lt;summary&gt;\r\n\/\/\/ \u8bfb\u53d6\u6587\u672c\u6587\u4ef6\r\n\/\/\/ &lt;\/summary&gt;\r\nprivate void ReadFromTxtFile()\r\n{\r\n    if(filePath.PostedFile.FileName != &quot;&quot;)\r\n    {\r\n        txtFilePath =filePath.PostedFile.FileName;\r\n        fileExtName = txtFilePath.Substring(txtFilePath.LastIndexOf(&quot;.&quot;)+1,3);\r\n\r\n        if(fileExtName !=&quot;txt&quot; &amp;&amp; fileExtName != &quot;TXT&quot;)\r\n        {\r\n            Response.Write(&quot;\u8bf7\u9009\u62e9\u6587\u672c\u6587\u4ef6&quot;);\r\n        }\r\n        else\r\n        {\r\n            StreamReader fileStream = new StreamReader(txtFilePath,Encoding.Default);\r\n            txtContent.Text = fileStream.ReadToEnd();\r\n            fileStream.Close();\r\n        }\r\n    }\r\n }\r\n<\/pre>\n<p>\u4e8c\uff0e\u83b7\u53d6\u6587\u4ef6\u5217\u8868<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/**\/\/\/\/ &lt;summary&gt;\r\n\/\/\/ \u83b7\u53d6\u6587\u4ef6\u5217\u8868\r\n\/\/\/ &lt;\/summary&gt;\r\nprivate void GetFileList()\r\n{\r\n    string strCurDir,FileName,FileExt;\r\n    \r\n    \/**\/\/\/\/\u6587\u4ef6\u5927\u5c0f\r\n    long FileSize;\r\n    \r\n    \/**\/\/\/\/\u6700\u540e\u4fee\u6539\u65f6\u95f4;\r\n    DateTime FileModify;\r\n\r\n    \/**\/\/\/\/\u521d\u59cb\u5316\r\n    if(!IsPostBack)\r\n    {\r\n        \/**\/\/\/\/\u521d\u59cb\u5316\u65f6,\u9ed8\u8ba4\u4e3a\u5f53\u524d\u9875\u9762\u6240\u5728\u7684\u76ee\u5f55\r\n        strCurDir = Server.MapPath(&quot;.&quot;);\r\n        lblCurDir.Text = strCurDir;\r\n        txtCurDir.Text = strCurDir;\r\n    }\r\n    else\r\n    {\r\n        strCurDir = txtCurDir.Text;\r\n        txtCurDir.Text = strCurDir;\r\n        lblCurDir.Text = strCurDir;\r\n    }\r\n    FileInfo fi;\r\n    DirectoryInfo dir;\r\n    TableCell td;\r\n    TableRow tr;\r\n    tr = new TableRow();\r\n    \r\n    \/**\/\/\/\/\u52a8\u6001\u6dfb\u52a0\u5355\u5143\u683c\u5185\u5bb9\r\n    td = new TableCell();\r\n    td.Controls.Add(new LiteralControl(&quot;\u6587\u4ef6\u540d&quot;));\r\n    tr.Cells.Add(td);\r\n    td = new TableCell();\r\n    td.Controls.Add(new LiteralControl(&quot;\u6587\u4ef6\u7c7b\u578b&quot;));\r\n    tr.Cells.Add(td);\r\n    td = new TableCell();\r\n    td.Controls.Add(new LiteralControl(&quot;\u6587\u4ef6\u5927\u5c0f&quot;));\r\n    tr.Cells.Add(td);\r\n    td = new TableCell();\r\n    td.Controls.Add(new LiteralControl(&quot;\u6700\u540e\u4fee\u6539\u65f6\u95f4&quot;));\r\n    tr.Cells.Add(td);\r\n\r\n    tableDirInfo.Rows.Add(tr);\r\n    \r\n    \/**\/\/\/\/\u9488\u5bf9\u5f53\u524d\u76ee\u5f55\u5efa\u7acb\u76ee\u5f55\u5f15\u7528\u5bf9\u8c61\r\n    DirectoryInfo dirInfo = new DirectoryInfo(txtCurDir.Text);\r\n    \r\n    \/**\/\/\/\/\u5faa\u73af\u5224\u65ad\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u548c\u76ee\u5f55\r\n    foreach(FileSystemInfo fsi in dirInfo.GetFileSystemInfos())\r\n    {\r\n        FileName = &quot;&quot;;\r\n        FileExt = &quot;&quot;;\r\n        FileSize = 0;\r\n        \r\n        \/**\/\/\/\/\u5982\u679c\u662f\u6587\u4ef6\r\n        if(fsi is FileInfo)\r\n        {\r\n            fi = (FileInfo)fsi;\r\n            \r\n            \/**\/\/\/\/\u53d6\u5f97\u6587\u4ef6\u540d\r\n            FileName = fi.Name;\r\n            \r\n            \/**\/\/\/\/\u53d6\u5f97\u6587\u4ef6\u7684\u6269\u5c55\u540d\r\n            FileExt = fi.Extension;\r\n            \r\n            \/**\/\/\/\/\u53d6\u5f97\u6587\u4ef6\u7684\u5927\u5c0f\r\n            FileSize = fi.Length;\r\n            \r\n            \/**\/\/\/\/\u53d6\u5f97\u6587\u4ef6\u7684\u6700\u540e\u4fee\u6539\u65f6\u95f4\r\n            FileModify = fi.LastWriteTime;\r\n        }\r\n\r\n        \/**\/\/\/\/\u5426\u5219\u662f\u76ee\u5f55\r\n        else\r\n        {\r\n            dir = (DirectoryInfo)fsi;\r\n            \r\n            \/**\/\/\/\/\u53d6\u5f97\u76ee\u5f55\u540d\r\n            FileName = dir.Name;\r\n            \r\n            \/**\/\/\/\/\u53d6\u5f97\u76ee\u5f55\u7684\u6700\u540e\u4fee\u6539\u65f6\u95f4\r\n            FileModify = dir.LastWriteTime;\r\n            \r\n            \/**\/\/\/\/\u8bbe\u7f6e\u6587\u4ef6\u7684\u6269\u5c55\u540d\u4e3a&quot;\u6587\u4ef6\u5939&quot;\r\n            FileExt = &quot;\u6587\u4ef6\u5939&quot;;\r\n        }\r\n        \r\n        \/**\/\/\/\/\u52a8\u6001\u6dfb\u52a0\u8868\u683c\u5185\u5bb9\r\n        tr = new TableRow();\r\n        td = new TableCell();\r\n        td.Controls.Add(new LiteralControl(FileName));\r\n        tr.Cells.Add(td);\r\n        td = new TableCell();\r\n        td.Controls.Add(new LiteralControl(FileExt));\r\n        tr.Cells.Add(td);\r\n        td = new TableCell();\r\n        td.Controls.Add(new LiteralControl(FileSize.ToString()+&quot;\u5b57\u8282&quot;));\r\n        tr.Cells.Add(td);\r\n        td = new TableCell();\r\n        td.Controls.Add(new LiteralControl(FileModify.ToString(&quot;yyyy-mm-dd hh:mm:ss&quot;)));\r\n        tr.Cells.Add(td);\r\n        tableDirInfo.Rows.Add(tr);\r\n    }\r\n}\r\n<\/pre>\n<p>\u4e09\uff0e\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/**\/\/\/\/ &lt;summary&gt;\r\n\/\/\/ \u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\r\n\/\/\/ &lt;\/summary&gt;\r\nprivate void ReadLogFile()\r\n{\r\n    \/**\/\/\/\/\u4ece\u6307\u5b9a\u7684\u76ee\u5f55\u4ee5\u6253\u5f00\u6216\u8005\u521b\u5efa\u7684\u5f62\u5f0f\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\r\n    FileStream fs = new FileStream(Server.MapPath(&quot;upedFile&quot;)+&quot;\\\\logfile.txt&quot;, FileMode.OpenOrCreate, FileAccess.Read);\r\n\r\n    \/**\/\/\/\/\u5b9a\u4e49\u8f93\u51fa\u5b57\u7b26\u4e32\r\n    StringBuilder output = new StringBuilder();\r\n    \r\n    \/**\/\/\/\/\u521d\u59cb\u5316\u8be5\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e3a0\r\n    output.Length = 0;\r\n    \r\n    \/**\/\/\/\/\u4e3a\u4e0a\u9762\u521b\u5efa\u7684\u6587\u4ef6\u6d41\u521b\u5efa\u8bfb\u53d6\u6570\u636e\u6d41\r\n    StreamReader read = new StreamReader(fs);\r\n    \r\n    \/**\/\/\/\/\u8bbe\u7f6e\u5f53\u524d\u6d41\u7684\u8d77\u59cb\u4f4d\u7f6e\u4e3a\u6587\u4ef6\u6d41\u7684\u8d77\u59cb\u70b9\r\n    read.BaseStream.Seek(0, SeekOrigin.Begin);\r\n    \r\n    \/**\/\/\/\/\u8bfb\u53d6\u6587\u4ef6\r\n    while (read.Peek() &gt; -1) \r\n    {\r\n        \/**\/\/\/\/\u53d6\u6587\u4ef6\u7684\u4e00\u884c\u5185\u5bb9\u5e76\u6362\u884c\r\n        output.Append(read.ReadLine() + &quot;\\n&quot;);\r\n    }\r\n    \r\n    \/**\/\/\/\/\u5173\u95ed\u91ca\u653e\u8bfb\u6570\u636e\u6d41\r\n    read.Close();\r\n    \r\n    \/**\/\/\/\/\u8fd4\u56de\u8bfb\u5230\u7684\u65e5\u5fd7\u6587\u4ef6\u5185\u5bb9\r\n    return output.ToString();\r\n}\r\n<\/pre>\n<p>\u56db\uff0e\u5199\u5165\u65e5\u5fd7\u6587\u4ef6<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/**\/\/\/\/ &lt;summary&gt;\r\n\/\/\/ \u5199\u5165\u65e5\u5fd7\u6587\u4ef6\r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;param name=&quot;input&quot;&gt;&lt;\/param&gt;\r\nprivate void WriteLogFile(string input)\r\n{    \r\n    \/**\/\/\/\/\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u7684\u76ee\u5f55\r\n    string fname = Server.MapPath(&quot;upedFile&quot;) + &quot;\\\\logfile.txt&quot;;\r\n    \/**\/\/\/\/\u5b9a\u4e49\u6587\u4ef6\u4fe1\u606f\u5bf9\u8c61\r\n    FileInfo finfo = new FileInfo(fname);\r\n\r\n    \/**\/\/\/\/\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u4ee5\u53ca\u662f\u5426\u5927\u4e8e2K\r\n    if ( finfo.Exists &amp;&amp; finfo.Length &gt; 2048 )\r\n    {\r\n        \/**\/\/\/\/\u5220\u9664\u8be5\u6587\u4ef6\r\n        finfo.Delete();\r\n    }\r\n    \/**\/\/\/\/\u521b\u5efa\u53ea\u5199\u6587\u4ef6\u6d41\r\n    using(FileStream fs = finfo.OpenWrite())\r\n    {\r\n        \/**\/\/\/\/\u6839\u636e\u4e0a\u9762\u521b\u5efa\u7684\u6587\u4ef6\u6d41\u521b\u5efa\u5199\u6570\u636e\u6d41\r\n        StreamWriter w = new StreamWriter(fs);\r\n        \r\n        \/**\/\/\/\/\u8bbe\u7f6e\u5199\u6570\u636e\u6d41\u7684\u8d77\u59cb\u4f4d\u7f6e\u4e3a\u6587\u4ef6\u6d41\u7684\u672b\u5c3e\r\n        w.BaseStream.Seek(0, SeekOrigin.End);\r\n        \r\n        \/**\/\/\/\/\u5199\u5165\u201cLog Entry : \u201d\r\n        w.Write(&quot;\\nLog Entry : &quot;);\r\n        \r\n        \/**\/\/\/\/\u5199\u5165\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\u5e76\u6362\u884c\r\n        w.Write(&quot;{0} {1} \\r\\n&quot;, DateTime.Now.ToLongTimeString(),\r\n            DateTime.Now.ToLongDateString());\r\n        \r\n        \/**\/\/\/\/\u5199\u5165\u65e5\u5fd7\u5185\u5bb9\u5e76\u6362\u884c\r\n        w.Write(input + &quot;\\n&quot;);\r\n        \r\n        \/**\/\/\/\/\u5199\u5165------------------------------------\u201c\u5e76\u6362\u884c\r\n        w.Write(&quot;------------------------------------\\n&quot;);\r\n        \r\n        \/**\/\/\/\/\u6e05\u7a7a\u7f13\u51b2\u533a\u5185\u5bb9\uff0c\u5e76\u628a\u7f13\u51b2\u533a\u5185\u5bb9\u5199\u5165\u57fa\u7840\u6d41\r\n        w.Flush();\r\n        \r\n        \/**\/\/\/\/\u5173\u95ed\u5199\u6570\u636e\u6d41\r\n        w.Close();\r\n    }\r\n}\r\n<\/pre>\n<p>\u4e94\uff0e\u521b\u5efaHTML\u6587\u4ef6<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/**\/\/\/\/ &lt;summary&gt;\r\n\/\/\/ \u521b\u5efaHTML\u6587\u4ef6\r\n\/\/\/ &lt;\/summary&gt;\r\nprivate void CreateHtmlFile()\r\n{    \r\n    \/**\/\/\/\/\u5b9a\u4e49\u548chtml\u6807\u8bb0\u6570\u76ee\u4e00\u81f4\u7684\u6570\u7ec4\r\n    string&#x5B;] newContent = new string&#x5B;5];\r\n    StringBuilder strhtml = new StringBuilder();\r\n    try \r\n    {\r\n        \/**\/\/\/\/\u521b\u5efaStreamReader\u5bf9\u8c61\r\n        using (StreamReader sr = new StreamReader(Server.MapPath(&quot;createHTML&quot;) + &quot;\\\\template.html&quot;)) \r\n        {\r\n            String oneline;\r\n            \r\n            \/**\/\/\/\/\u8bfb\u53d6\u6307\u5b9a\u7684HTML\u6587\u4ef6\u6a21\u677f\r\n            while ((oneline = sr.ReadLine()) != null) \r\n            {\r\n                strhtml.Append(oneline);\r\n            }\r\n            sr.Close();\r\n        }\r\n    }\r\n    catch(Exception err)\r\n    {\r\n        \/**\/\/\/\/\u8f93\u51fa\u5f02\u5e38\u4fe1\u606f\r\n        Response.Write(err.ToString());\r\n    }\r\n    \/**\/\/\/\/\u4e3a\u6807\u8bb0\u6570\u7ec4\u8d4b\u503c\r\n    newContent&#x5B;0] = txtTitle.Text;\/\/\u6807\u9898\r\n    newContent&#x5B;1] = &quot;BackColor='#cccfff'&quot;;\/\/\u80cc\u666f\u8272\r\n    newContent&#x5B;2] = &quot;#ff0000&quot;;\/\/\u5b57\u4f53\u989c\u8272\r\n    newContent&#x5B;3] = &quot;100px&quot;;\/\/\u5b57\u4f53\u5927\u5c0f\r\n    newContent&#x5B;4] = txtContent.Text;\/\/\u4e3b\u8981\u5185\u5bb9\r\n\r\n    \/**\/\/\/\/\u6839\u636e\u4e0a\u9762\u65b0\u7684\u5185\u5bb9\u751f\u6210html\u6587\u4ef6\r\n    try\r\n    {\r\n        \/**\/\/\/\/\u6307\u5b9a\u8981\u751f\u6210\u7684HTML\u6587\u4ef6\r\n        string fname = Server.MapPath(&quot;createHTML&quot;) +&quot;\\\\&quot; + DateTime.Now.ToString(&quot;yyyymmddhhmmss&quot;) + &quot;.html&quot;;\r\n        \r\n        \/**\/\/\/\/\u66ff\u6362html\u6a21\u7248\u6587\u4ef6\u91cc\u7684\u6807\u8bb0\u4e3a\u65b0\u7684\u5185\u5bb9\r\n        for(int i=0;i &lt; 5;i++)\r\n        {\r\n            strhtml.Replace(&quot;$htmlkey&#x5B;&quot;+i+&quot;]&quot;,newContent&#x5B;i]);\r\n        }\r\n        \/**\/\/\/\/\u521b\u5efa\u6587\u4ef6\u4fe1\u606f\u5bf9\u8c61\r\n        FileInfo finfo = new FileInfo(fname);\r\n        \r\n        \/**\/\/\/\/\u4ee5\u6253\u5f00\u6216\u8005\u5199\u5165\u7684\u5f62\u5f0f\u521b\u5efa\u6587\u4ef6\u6d41\r\n        using(FileStream fs = finfo.OpenWrite())\r\n        {\r\n            \/**\/\/\/\/\u6839\u636e\u4e0a\u9762\u521b\u5efa\u7684\u6587\u4ef6\u6d41\u521b\u5efa\u5199\u6570\u636e\u6d41\r\n            StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.GetEncoding(&quot;GB2312&quot;));\r\n            \r\n            \/**\/\/\/\/\u628a\u65b0\u7684\u5185\u5bb9\u5199\u5230\u521b\u5efa\u7684HTML\u9875\u9762\u4e2d\r\n            sw.WriteLine(strhtml);\r\n            sw.Flush();\r\n            sw.Close();\r\n        }\r\n        \r\n        \/**\/\/\/\/\u8bbe\u7f6e\u8d85\u7ea7\u94fe\u63a5\u7684\u5c5e\u6027\r\n        hyCreateFile.Text = DateTime.Now.ToString(&quot;yyyymmddhhmmss&quot;)+&quot;.html&quot;;\r\n        hyCreateFile.NavigateUrl = &quot;createHTML\/&quot;+DateTime.Now.ToString(&quot;yyyymmddhhmmss&quot;)+&quot;.html&quot;;\r\n    }\r\n    catch(Exception err)\r\n    { \r\n        Response.Write (err.ToString());\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C#\u64cd\u4f5c\u6587\u672c\u6587\u4ef6\u7684\u7c7bStreamReader \u53ca StreamWriter \u8fd9\u4e24\u4e2a\u7c7b\u4e3b\u8981\u662f\u7528\u662f\u6587\u672c\u6587\u4ef6\u7684\u64cd\u4f5c\uff0c [&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-542","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/542","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=542"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/542\/revisions"}],"predecessor-version":[{"id":4627,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/542\/revisions\/4627"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}