usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;//kangjiausingSystem.IO;usingSystem.Runtime.Serialization;//序列化usingSystem.Runtime.Serialization.Formatters;usingSystem.Runtime.Serialization.Formatters.Binary; namespaceDay14IO{classIOHelper{///summary///使用静态类File来打开获得reader对象///再读取文件的内容////summary///paramname=path读取文件的路径/param///returns/returnspublicstaticstringGetStreamReadeString(stringpath){StreamReaderreader=File.OpenText(path);stringstrReade=reader.ReadToEnd();reader.Close();returnstrReade; }///summary///使用StreamReader读取指定文件的内容////summary///paramname=path读取指定文件的路径/param///paramname=mode打开文件的方式FileMode/param///paramname=Falge打开文件的方式FileMode/param///returns读取出的文件内容/returnspublicstaticstringGetStreamReadeString(stringpath,FileModemode,intFalge){FileStreamfsr;StreamReadersreader;stringstrReade=;try{fsr=newFileStream(path,mode);sreader=newStreamReader(fsr);switch(Falge){case1:strReade=sreader.ReadToEnd();break;case2:strReade=sreader.ReadLine();break;}}catch(IOException){returnnull;}sreader.Close();fsr.Close();returnstrReade;}///summary///使用BinaryReader读取指定文件////summary///paramname=path读取指定文件的路径path/param///paramname=mode打开文件的方式FileMode/param///returns读取出的文件内容/returnspublicstaticstringGetBinaryReadString(stringpath,FileModemode){FileStreamfsr=newFileStream(path,mode);BinaryReaderbr=newBinaryReader(fsr);stringstrBinaryReade=br.ReadString();br.Close();fsr.Close();returnstrBinaryReade;}///summary///将序列化的文件反序列化///并且将结果返回object////summary///paramname=path反序列化的文件的路径/param///paramname=mode指定系统打开文件的方式/param///returnsobject/returnspublicstaticobjectBinaryFormatterDeSerialize(stringpath,FileModemode){objectobjSender=newobject();if(File.Exists(path)){FileStreamfs=newFileStream(path,mode);try{BinaryFormatterBf=newBinaryFormatter();objSender=Bf.Deserialize(fs);}catch(SerializationException){returnnull;}finally{fs.Close();}}else{returnnull;}returnobjSender;} ///summary///将对象序列化到指定的文件////summary///paramname=path指定文件的路径/param///paramname=mode指定系统打开文件的方式/param///paramname=sender要序列化的对象/param///returns/returnspublicstaticboolBinaryFormatterSerialize(stringpath,FileModemode,objectsender){boolboolSave=false;FileStreamfs=newFileStream(path,FileMode.Create);BinaryFormatterBf=newBinaryFormatter();try{Bf.Serialize(fs,sender);boolSave=true;}catch(SerializationException){returnboolSave;}finally{fs.Close();}returnboolSave;}///summary///使用StreamWriter写入指定文件////summary///paramname=path写入指定文件的路径/param///paramname=mode制定系统打开文件的方式/param///paramname=writeString要写入文件的字符串/param///returns/returnspublicstaticboolStreamWrite(stringpath,FileModemode,stringwriteString){FileStreamfs;StreamWritersw;try{fs=newFileStream(path,mode);sw=newStreamWriter(fs);sw.Write(writeString);}catch(IOException){returnfalse;}sw.Close();fs.Close();returntrue;}///summary///使用BinaryWriter写入指定的文件////summary///paramname=path写入指定的文件的路径/param///paramname=mode指定系统打开文件的方式FileMode/param///paramname=writeString要写入文件的字符串/param///returns/returnspublicstaticboolStreamBinaryWrite(stringpath,FileModemode,stringwriteString){FileStreamfs;BinaryWriterbw;try{fs=newFileStream(path,mode);bw=newBinaryWriter(fs);bw.Write(writeString);}catch(IOException){returnfalse;}bw.Close();fs.Close();returntrue;} ///summary///删除指定的文件///此种删除是彻底删除无法恢复////summary///paramname=path要删除文件的路径/param///returns/returnspublicstaticboolStreamDeleteFile(stringpath){booltemp=false;if(File.Exists(path)){File.Delete(path);}else{returntemp;}temp=true;returntemp;}///summary///删除指定的文件夹///此种删除是彻底删除无法恢复////summary///paramname=path要删除文件夹的路径/param///returns/returnspublicstaticboolStreamDeleteDirectory(stringpath){booltemp=false;if(Directory.Exists(path)){Directory.Delete(path);}else{returntemp;}temp=true;returntemp;}}} 治疗白癜风的最好医院白癜风的治疗转载请注明原文网址:http://www.helimiaopu.com/cxkf/3709.html |