午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    Asp.net(C#)文件操作函數(shù)大全
    來源:易賢網(wǎng) 閱讀:975 次 日期:2014-09-29 10:40:03
    溫馨提示:易賢網(wǎng)小編為您整理了“Asp.net(C#)文件操作函數(shù)大全”,方便廣大網(wǎng)友查閱!

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

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

    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

    {

    構(gòu)造函數(shù)

    IDisposable 成員

    取得文件后綴名

    #region 寫文件

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

    * 函數(shù)名稱:WriteFile

    * 功能說明:當文件不存時,則創(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

    * 功能說明:讀取文本內(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

    * 功能說明:追加文件內(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

    * 功能說明:拷貝文件

    * 參 數(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

    * 功能說明:刪除文件

    * 參 數(shù):Path:文件路徑

    * 調(diào)用示列:

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

    更多信息請查看IT技術(shù)專欄

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機網(wǎng)站地址:Asp.net(C#)文件操作函數(shù)大全

    2025國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)