网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 Asp.net(C#)文件操作函数大全
内容
    对于文件流的操作,首先你得引用命名空间: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");
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/15 13:39:56