国产探花免费观看_亚洲丰满少妇自慰呻吟_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 追加文件

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通州区| 朝阳区| 禄丰县| 昌江| 黄龙县| 娄底市| 峡江县| 临夏县| 永丰县| 华安县| 松原市| 淮南市| 苗栗市| 类乌齐县| 永胜县| 永济市| 泸定县| 海阳市| 西和县| 阳东县| 全椒县| 佳木斯市| 兰坪| 邳州市| 靖边县| 乾安县| 黄龙县| 舟曲县| 泰顺县| 开鲁县| 惠来县| 天全县| 南投县| 多伦县| 盐城市| 闽侯县| 楚雄市| 斗六市| 枣庄市| 磐安县| 阳山县|