欢迎来到天天文库
浏览记录
ID:26409087
大小:279.00 KB
页数:133页
时间:2018-11-26
《c#文件操作大全》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C#文件操作大全上一篇/下一篇 2010-05-0710:23:27/个人分类:.net查看(1535)/评论(20)/评分(79/1)1.创建文件夹//usingSystem.IO;Directory.CreateDirectory(%%1);2.创建文件//usingSystem.IO;File.Create(%%1);3.删除文件//usingSystem.IO;File.Delete(%%1);4.删除文件夹//usingSystem.IO;Directory.Delete(%%1);5.删除一
2、个目录下所有的文件夹//usingSystem.IO;foreach(stringdirStrinDirectory.GetDirectories(%%1)){ DirectoryInfodir=newDirectoryInfo(dirStr); ArrayListfolders=newArrayList(); FileSystemInfo[]fileArr=dir.GetFileSystemInfos(); for(inti=0;i3、lders[i]asFileInfo; if(f==null) { DirectoryInfod=folders[i]asDirectoryInfo; d.Delete(); } }}6.清空文件夹//usingSystem.IO;Directory.Delete(%%1,true);Directory.CreateDirectory(%%1);7.读取文件//usingSystem.IO;StreamReaders=File.OpenText(%%1);string%%2=null;wh4、ile((%%2=s.ReadLine())!=null){ %%3}s.Close();8.写入文件//usingSystem.IO;FileInfof=newFileInfo(%%1);StreamWriterw=f.CreateText();w.WriteLine(%%2);w.Close();9.写入随机文件//usingSystem.IO;byte[]dataArray=newbyte[100000];//newRandom().NextBytes(dataArray);using(FileS5、treamFileStream=newFileStream(%%1,FileMode.Create)){//Writethedatatothefile,bytebybyte. for(inti=0;i6、verifythedata. for(inti=0;i7、ibutes.ReadOnly)!=0){ %%2}else{ %%3}11.写入属性//usingSystem.IO;FileInfof=newFileInfo(%%1);//设置只读f.Attributes=myFile.Attributes8、FileAttributes.ReadOnly;//设置可写f.Attributes=myFile.Attributes&~FileAttributes.ReadOnly;12.枚举一个文件夹中的所有文件夹//usingSystem.IO;foreach(st9、ring%%2inDirectory.GetDirectories(%%1)){ %%3}/*DirectoryInfodir=newDirectoryInfo(%%1);FileInfo[]files=dir.GetFiles("*.*");foreach(FileInfo%%2infiles){ %%3}*/13.复制文件夹/*usingSystem.IO;usingSystem.Collections;*/stringpath=(%%
3、lders[i]asFileInfo; if(f==null) { DirectoryInfod=folders[i]asDirectoryInfo; d.Delete(); } }}6.清空文件夹//usingSystem.IO;Directory.Delete(%%1,true);Directory.CreateDirectory(%%1);7.读取文件//usingSystem.IO;StreamReaders=File.OpenText(%%1);string%%2=null;wh
4、ile((%%2=s.ReadLine())!=null){ %%3}s.Close();8.写入文件//usingSystem.IO;FileInfof=newFileInfo(%%1);StreamWriterw=f.CreateText();w.WriteLine(%%2);w.Close();9.写入随机文件//usingSystem.IO;byte[]dataArray=newbyte[100000];//newRandom().NextBytes(dataArray);using(FileS
5、treamFileStream=newFileStream(%%1,FileMode.Create)){//Writethedatatothefile,bytebybyte. for(inti=0;i6、verifythedata. for(inti=0;i7、ibutes.ReadOnly)!=0){ %%2}else{ %%3}11.写入属性//usingSystem.IO;FileInfof=newFileInfo(%%1);//设置只读f.Attributes=myFile.Attributes8、FileAttributes.ReadOnly;//设置可写f.Attributes=myFile.Attributes&~FileAttributes.ReadOnly;12.枚举一个文件夹中的所有文件夹//usingSystem.IO;foreach(st9、ring%%2inDirectory.GetDirectories(%%1)){ %%3}/*DirectoryInfodir=newDirectoryInfo(%%1);FileInfo[]files=dir.GetFiles("*.*");foreach(FileInfo%%2infiles){ %%3}*/13.复制文件夹/*usingSystem.IO;usingSystem.Collections;*/stringpath=(%%
6、verifythedata. for(inti=0;i7、ibutes.ReadOnly)!=0){ %%2}else{ %%3}11.写入属性//usingSystem.IO;FileInfof=newFileInfo(%%1);//设置只读f.Attributes=myFile.Attributes8、FileAttributes.ReadOnly;//设置可写f.Attributes=myFile.Attributes&~FileAttributes.ReadOnly;12.枚举一个文件夹中的所有文件夹//usingSystem.IO;foreach(st9、ring%%2inDirectory.GetDirectories(%%1)){ %%3}/*DirectoryInfodir=newDirectoryInfo(%%1);FileInfo[]files=dir.GetFiles("*.*");foreach(FileInfo%%2infiles){ %%3}*/13.复制文件夹/*usingSystem.IO;usingSystem.Collections;*/stringpath=(%%
7、ibutes.ReadOnly)!=0){ %%2}else{ %%3}11.写入属性//usingSystem.IO;FileInfof=newFileInfo(%%1);//设置只读f.Attributes=myFile.Attributes
8、FileAttributes.ReadOnly;//设置可写f.Attributes=myFile.Attributes&~FileAttributes.ReadOnly;12.枚举一个文件夹中的所有文件夹//usingSystem.IO;foreach(st
9、ring%%2inDirectory.GetDirectories(%%1)){ %%3}/*DirectoryInfodir=newDirectoryInfo(%%1);FileInfo[]files=dir.GetFiles("*.*");foreach(FileInfo%%2infiles){ %%3}*/13.复制文件夹/*usingSystem.IO;usingSystem.Collections;*/stringpath=(%%
此文档下载收益归作者所有