国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

Asp.net(C# )文件操作函數大全

2019-11-02 14:54:19
字體:
來源:轉載
供稿:網友

   對于文件流的操作,首先你得引用命名空間:using System.IO;對文件的操作主要指兩方面:第一,是對文件本身進行操作;第二,是對文件內容進行操作。

  如果是前者,樓主可以使用System.IO.FileInfo等類型,對文件進行操作;后者的話可以通過System.IO.StreamReader,StreamWriter,FileStreamd等流對象對文件內容進行操作。

  Asp.net(C#)對文件操作的方法(讀取,刪除,批量拷貝,刪除...)

  using System.Data;

  using System.Configuration;

  using System.Web;

  using System.Web.Security;

  using System.Web.UI;

  using System.Web.UI.WebControls;

  using System.Web.UI.WebControls.WebParts;

  using System.Web.UI.HtmlControls;

  using System.Text;

  using System.IO;

  namespace EC

  {

  ///

  /// FileObj 的摘要說明

  ///

  public class FileObj

  {

  構造函數

  IDisposable 成員

  取得文件后綴名

  #region 寫文件

  /****************************************

  * 函數名稱:WriteFile

  * 功能說明:當文件不存時,則創建文件,并追加文件

  * 參 數:Path:文件路徑,Strings:文本內容

  * 調用示列:

  * string Path = Server.MapPath("Default2.aspx");

  * string Strings = "這是我寫的內容啊";

  * EC.FileObj.WriteFile(Path,Strings);

  *****************************************/

  ///

  /// 寫文件

  ///

  /// 文件路徑

  /// 文件內容

  public static void WriteFile(string Path, string Strings)

  {

  if (!System.IO.File.Exists(Path))

  {

  //Directory.CreateDirectory(Path);

  System.IO.FileStream f = System.IO.File.Create(Path);

  f.Close();

  f.Dispose();

  }

  System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);

  f2.WriteLine(Strings);

  f2.Close();

  f2.Dispose();

  }

  #endregion

  #region 讀文件

  /****************************************

  * 函數名稱:ReadFile

  * 功能說明:讀取文本內容

  * 參 數:Path:文件路徑

  * 調用示列:

  * string Path = Server.MapPath("Default2.aspx");

  * string s = EC.FileObj.ReadFile(Path);

  *****************************************/

  ///

  /// 讀文件

  ///

  /// 文件路徑

  ///

  public static string ReadFile(string Path)

  {

  string s = "";

  if (!System.IO.File.Exists(Path))

  s = "不存在相應的目錄";

  else

  {

  StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));

  s = f2.ReadToEnd();

  f2.Close();

  f2.Dispose();

  }

  return s;

  }

  #endregion

  #region 追加文件

  /****************************************

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 全州县| 那坡县| 开鲁县| 通城县| 综艺| 花莲县| 兰西县| 北安市| 岳阳市| 沙坪坝区| 阳城县| 广宁县| 漯河市| 陆河县| 连州市| 两当县| 五家渠市| 恩施市| 望谟县| 永安市| 茌平县| 武山县| 邯郸市| 荆州市| 武功县| 什邡市| 合山市| 岳阳县| 从化市| 南郑县| 冕宁县| 平原县| 南开区| 西畴县| 锡林郭勒盟| 蛟河市| 永年县| 萨迦县| 双柏县| 纳雍县| 正蓝旗|