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

字號(hào):


    對(duì)于文件流的操作,首先你得引用命名空間:using System.IO;對(duì)文件的操作主要指兩方面:第一,是對(duì)文件本身進(jìn)行操作;第二,是對(duì)文件內(nèi)容進(jìn)行操作。
    如果是前者,樓主可以使用System.IO.FileInfo等類型,對(duì)文件進(jìn)行操作;后者的話可以通過(guò)System.IO.StreamReader,StreamWriter,FileStreamd等流對(duì)象對(duì)文件內(nèi)容進(jìn)行操作。
    Asp.net(C#)對(duì)文件操作的方法(讀取,刪除,批量拷貝,刪除...)
    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 的摘要說(shuō)明
    ///
    public class FileObj
    {
    構(gòu)造函數(shù)
    IDisposable 成員
    取得文件后綴名
    #region 寫文件
    /****************************************
    * 函數(shù)名稱:WriteFile
    * 功能說(shuō)明:當(dāng)文件不存時(shí),則創(chuàng)建文件,并追加文件
    * 參 數(shù):Path:文件路徑,Strings:文本內(nèi)容
    * 調(diào)用示列:
    * string Path = Server.MapPath("Default2.aspx");
    * string Strings = "這是我寫的內(nèi)容啊";
    * EC.FileObj.WriteFile(Path,Strings);
    *****************************************/
    ///
    /// 寫文件
    ///
    /// 文件路徑
    /// 文件內(nèi)容
    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 讀文件
    /****************************************
    * 函數(shù)名稱:ReadFile
    * 功能說(shuō)明:讀取文本內(nèi)容
    * 參 數(shù):Path:文件路徑
    * 調(diào)用示列:
    * 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 = "不存在相應(yīng)的目錄";
    else
    {
    StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));
    s = f2.ReadToEnd();
    f2.Close();
    f2.Dispose();
    }
    return s;
    }
    #endregion
    #region 追加文件
    /****************************************
    * 函數(shù)名稱:FileAdd
    * 功能說(shuō)明:追加文件內(nèi)容
    * 參 數(shù):Path:文件路徑,strings:內(nèi)容
    * 調(diào)用示列:
    * string Path = Server.MapPath("Default2.aspx");
    * string Strings = "新追加內(nèi)容";
    * EC.FileObj.FileAdd(Path, Strings);
    *****************************************/
    ///
    /// 追加文件
    ///
    /// 文件路徑
    /// 內(nèi)容
    public static void FileAdd(string Path, string strings)
    {
    StreamWriter sw = File.AppendText(Path);
    sw.Write(strings);
    sw.Flush();
    sw.Close();
    sw.Dispose();
    }
    #endregion
    #region 拷貝文件
    /****************************************
    * 函數(shù)名稱:FileCoppy
    * 功能說(shuō)明:拷貝文件
    * 參 數(shù):OrignFile:原始文件,NewFile:新文件路徑
    * 調(diào)用示列:
    * 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 刪除文件
    /****************************************
    * 函數(shù)名稱:FileDel
    * 功能說(shuō)明:刪除文件
    * 參 數(shù):Path:文件路徑
    * 調(diào)用示列:
    * string Path = Server.MapPath("Default3.aspx");