asp.net(c#)文件操作函數(shù)大全

字號:


    對于文件流的操作,首先你得引用命名空間: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
    {
    構造函數(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 = 不存在相應的目錄;
    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);