• 爱情文章
  • 亲情文章
  • 友情文章
  • 生活随笔
  • 校园文章
  • 经典文章
  • 人生哲理
  • 励志文章
  • 搞笑文章
  • 心情日记
  • 英语文章
  • 范文大全
  • 作文大全
  • 新闻阅读
  • 当前位置: 山茶花美文网 > 新闻阅读 > 正文

    Asp.net(C#)文件操作函数大全|维生素c片怎么样

    时间:2020-05-27来源:山茶花美文网 本文已影响 山茶花美文网手机站

    对于文件流的操作,首先你得引用命名空间:using System.IO;对文件的操作主要指两方面:第一,是对文件本身进行操作;第二,是对文件内容进行操作。

    如果是前者,楼主可以使用System.IO.FileInfo等类型,对文件进行操作;后者的话可以通过System.IO.StreamReader,StreamWriter,FileStreamd等流对象对文件内容进行操作。

    (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");

    • Asp.net(C#)文件操作函数大全|维生素c片怎么样 相关文章:
    • 爱情文章
    • 亲情文章
    • 友情文章
    • 随笔
    • 哲理
    • 励志
    • 范文大全