标题 | 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); |
随便看 |
|
在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。