Asp.net(C#)檔案操作函數大全

來源:互聯網
上載者:User

   對於檔案流的操作,首先你得引用命名空間: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 追加檔案

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

  * 函數名稱:FileAdd

  * 功能說明:追加檔案內容

  * 參 數:Path:檔案路徑,strings:內容

  * 調用示列:

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

  * string Strings = "新追加內容";

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

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

  ///

  /// 追加檔案

  ///

  /// 檔案路徑

  /// 內容

  public static void FileAdd(string Path, string strings)

  {

  StreamWriter sw = File.AppendText(Path);

  sw.Write(strings);

  sw.Flush();

  sw.Close();

  sw.Dispose();

  }

  #endregion

  #region 拷貝檔案

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

  * 函數名稱:FileCoppy

  * 功能說明:拷貝檔案

  * 參 數:OrignFile:原始檔案,NewFile:新檔案路徑

  * 調用示列:

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

  * string NewFile = Server.MapPath("Default3.aspx");

  * EC.FileObj.FileCoppy(OrignFile, NewFile);

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

  ///

  /// 拷貝檔案

  ///

  /// 原始檔案

  /// 新檔案路徑

  public static void FileCoppy(string OrignFile, string NewFile)

  {

  File.Copy(OrignFile, NewFile, true);

  }

  #endregion

  #region 刪除檔案

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

  * 函數名稱:FileDel

  * 功能說明:刪除檔案

  * 參 數:Path:檔案路徑

  * 調用示列:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.