您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 给排水/暖通与智能化 > C#通用文件帮助类
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;namespaceHelpProject{publicclassIOHelper{#region创建文件夹///summary///创建文件夹////summary///paramname=dirName/param///returns/returnspublicstaticboolCreateDir(stringdirName){if(!Directory.Exists(dirName)){Directory.CreateDirectory(dirName);}returntrue;}#endregion#region复制文件夹///summary///复制文件夹////summary///paramname=fromDir被复制的目录/param///paramname=toDir复制到的目录/param///returns/returnspublicstaticboolCopyDir(stringfromDir,stringtoDir){if(fromDir==null||toDir==null){thrownewNullReferenceException(参数为空);}if(fromDir==toDir){thrownewException(两个目录都是+fromDir);}if(!Directory.Exists(fromDir)){thrownewIOException(目录fromDir=+fromDir+不存在);}DirectoryInfodir=newDirectoryInfo(fromDir);returnCopyDir(dir,toDir,dir.FullName);}///summary///复制目录////summary///paramname=fromDir被复制的目录/param///paramname=toDir复制到的目录/param///paramname=rootDir被复制的根目录/param///returns/returnsprivatestaticboolCopyDir(DirectoryInfofromDir,stringtoDir,stringrootDir){stringfilePath=string.Empty;foreach(FileInfofinfromDir.GetFiles()){filePath=toDir+f.FullName.Substring(rootDir.Length);stringnewDir=filePath.Substring(0,filePath.LastIndexOf(//));CreateDir(newDir);File.Copy(f.FullName,filePath,true);}foreach(DirectoryInfodirinfromDir.GetDirectories()){CopyDir(dir,toDir,rootDir);}returntrue;}#endregion#region文件是否存在///summary///判断文件是否存在////summary///paramname=fileName/param///returns/returnspublicstaticboolExists(stringfileName){if(fileName==null||fileName.Trim()==){returnfalse;}if(File.Exists(fileName)){returntrue;}returnfalse;}#endregion#region删除文件夹///summary///删除文件夹////summary///paramname=dir制定目录/param///paramname=onlyDir是否只删除目录/param///returns/returnspublicstaticboolDeleteDir(stringdir,boolonlyDir){if(dir==null||dir.Trim()==){thrownewNullReferenceException(目录dir=+dir+不存在);}if(!Directory.Exists(dir)){returnfalse;}DirectoryInfodirInfo=newDirectoryInfo(dir);if(dirInfo.GetFiles().Length==0&&dirInfo.GetDirectories().Length==0){Directory.Delete(dir);returntrue;}if(!onlyDir){returnfalse;}else{DeleteDir(dirInfo);returntrue;}}publicstaticvoidDeleteDir(DirectoryInfodir){if(dir==null){thrownewNullReferenceException(目录不存在);}foreach(DirectoryInfodindir.GetDirectories()){DeleteDir(d);}foreach(FileInfofindir.GetFiles()){DeleteFile(f.FullName);}dir.Delete();}#endregion#region创建文件///summary///创建文件////summary///paramname=fileName文件名称/param///returns/returnspublicstaticboolCreateFile(stringfileName){if(!File.Exists(fileName)){FileStreamfs=File.Create(fileName);fs.Close();fs.Dispose();}returnFile.Exists(fileName);}#endregion#region写文件/*****************************************函数名称:WriteFile*功能说明:写文件,会覆盖掉以前的内容*参数:Path:文件路径,Strings:文本内容*调用示列:*stringPath=Server.MapPath(Default2.aspx);*stringStrings=这是我写的内容啊;*EC.FileObj.WriteFile(Path,Strings);*****************************************////summary///写文件////summary///paramname=Path文件路径/param///paramname=Strings文件内容/parampublicstaticboolWriteFile(stringPath,stringStrings){if(!System.IO.File.Exists(Path)){System.IO.FileStreamf=System.IO.File.Create(Path);f.Close();}System.IO.StreamWriterf2=newSystem.IO.StreamWriter(Path,false,System.Text.Encoding.UTF8);f2.Write(Strings);f2.Close();f2.Dispose();returnFile.Exists(Path);}///summary///写入一行////summary///paramname=fileName文件名/param///paramname=content内容/param///returns/returnspublicstaticboolWriteLine(stringfileName,stringcontent){using(FileStreamfs=newFileStream(fileName,FileMode.OpenOrCreate|FileMode.Append)){lock(fs){if(!fs.CanWrite){thrownewSystem.Security.SecurityException(文件fileName=+fileName+是只读文件不能写入!);}StreamWritersw=newStreamWriter(fs);sw.WriteLine(content);sw.Dispose();sw.Close();returntrue;}}}publicstaticboolCopyDir(DirectoryInfofromDir,stringtoDir){returnCopyDir(fromDir,toDir,fromDir.FullName);}#endregion#region追加文件/*****************************************函数名称:FileAdd*功能说明:追加文件内容*参数:Path:文件路径,strings:内容*调用示列:*stringPath=Server.MapPath(Default2.aspx);*stringStrings=新追加内容;*EC.FileObj.FileAdd(Path,Strings);*****************************************////summary///追加文件////summary///paramname=Path文件路径/param///paramname=strings内容/parampublicstaticvoidFileAdd(stringPath,stringstrings){StreamWritersw=File.AppendText(Path);sw.Write(strings);sw.Flush();sw.Close();}#endregion#region读文件/*****************************************函数名称:ReadFile*功能说明:读取文本内容*参数:Path:文件路径*调用示列:*stringPath=Server.MapPath(Default2.aspx);*strings=EC.FileObj.ReadFile(Path);*****************************************////summary///读文件////summary///paramname=Path文件路径/param///returns/returnspublicstaticstringReadFile(stringPath){strings=;if(!System.IO.File.Exists(Path))s=不存在相应的目录;else{StreamReaderf2=newStreamReader(Path,System.Text.Encoding.GetEncoding(gb2312));s=f2.ReadToEnd();f2.Close();f2.Dispose();}returns;}publicstaticstringReadLine(stringfileName){if(!Exists(fileName)){returnnull;}using(FileStreamfs=ne
本文标题:C#通用文件帮助类
链接地址:https://www.777doc.com/doc-4699639 .html