C#文件操作大全_第1页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、c#文件操作大全/using system.io; foreach (sing dirstr in directory.getdirectories(%1) directoryinfo dir = new directoryinfo(dirstr); arraylist ers=new arraylist(); filesysteminfo arr = dir.getfilesysteminfos(); for (int i = 0; i folders.count; i+) fileinfo f = foldersi as fileinfo; if (f = null) directoryi

2、nfo d = foldersi as directoryinfo; d.delete(); 6.清空文件夹 /using system.io; directory.delete(%1,true); directory.createdirectory(%1); 7.读取文件 7.1.操作系统默认编码 /using system.io; streamreader s = file.opentt(%1); string %2 = null; while (%2 = s.readline() != null) %3 s.close(); 7.2.utf-8编码 /* using system.io;

3、 using system.text; */ streamreader srfile = new streamreader(%1,eoding.utf8); while (string %2 = srfile.readline() != null) %3 srfile.close(); 7.3.分块读取 /* using system.io; using system.text; */ try filestream fs=new filestream(%1,filemode.open,fileaccess.read); binaryreader br=new binaryreader(fs,n

4、ew asciiencoding(); byte chunk; do chunk=br.readbytes(10240); if(chunk.length 0) %2 /chunk,chunk.length while(chunk.length fs.close(); ch /return -1; 8.写入文件 /using system.io; fileinfo f = new fileinfo(%1); streamwriter w = f.createtext(); w.writeline(%2); w.flush(); w.close(); 9.写入随机文件 /using system

5、.io; byte dataarray = new byte100000;/new random().nextbytes(dataarray); using(filestream filestream = new filestream(%1, filemode.create) / write the data to the file, byte by byte. for(int i = 0; i dataarray.length; i+) filestream.writebyte(dataarrayi); / set the stream position to the beginning o

6、f the file. filestream.seek(0, seekorigin.begin); / read and verify the data. for(int i = 0; i filestream.length; i+) if(dataarrayi != filestream.readbyte() /写入数据错误 return; / 数据流 +filestream.name+ 已验证 10.读取文件属性 /using system.io; fileinfo f = new fileinfo(%1);/f.creationtime,f.fullname if(f.attribute

7、s fileattributes.readonly) != 0) %2 ee %3 11.写入属性 /using system.io; fileinfo f = new fileinfo(%1); /设置只读 f.attributes = myfile.attributes | fileattributes.readonly; /设置可写 f.attributes = myfile.attributes fileattributes.readonly; 12.枚举一个文件夹中的全部文件夹 /using system.io; foreach (string %2 in directory.get

8、directories(%1) %3 /* directoryinfo dir = new directoryinfo(%1); fileinfo files = dir.getfiles( *.* foreach(fileinfo %2 in files) %3 */ 13.复制文件夹 /* using system.io; using system.collections; */ string path = (%2.lastindexof(path.directoryseparatorchar) = %2.length - 1) ? %2 : %2+path.directorysepara

9、torchar; string parent = path.getdirectoryname(%1); directory.createdirectory(path + path.getfilename(%1); directoryinfo dir = new directoryinfo(%1.lastindexof(path.directoryseparatorchar) = %1.length - 1) ? %1 : %1 + path.directoryseparatorchar); filesysteminfo filearr = dir.getfilesysteminfos(); q

10、ueue filesysteminfo folders = new queue filesysteminfo (dir.getfilesysteminfos(); while (folders.count 0) filesysteminfo tmp = folders.dequeue(); fileinfo f = tmp as fileinfo; if (f = null) directoryinfo d = tmp as directoryinfo; directory.createdirectory(d.fullname.replace(parent.lastindexof(path.d

11、irectoryseparatorchar) = parent.length - 1) ? parent : parent + path.directoryseparatorchar, path); foreach (filesysteminfo fi in d.getfilesysteminfos() folders.enqueue(fi); else f.copyto(f.fullname.replace(parent, path); 14.复制名目下全部的文件夹到另一个文件夹下 /* using system.io; using system.collections; */ direct

12、oryinfo d = new directoryinfo(%1); foreach (directoryinfo in d.getdirectories() queue filesysteminfo al = new queue filesysteminfo (dirs.getfilesysteminfos(); while (al.count 0) filesysteminfo temp = al.dequeue(); fileinfo file = temp as fileinfo; if (file = null) directoryinfo directory = temp as d

13、irectoryinfo; directory.createdirectory(path + directory.name); foreach (filesysteminfo fsi in directory.getfilesysteminfos() al.enqueue(fsi); else file.copy(file.fullname, path + file.name); 15.移动文件夹 /* using system.io; using system.collections; */ string filename = path.getfilename(%1); string pat

14、h=(%2.lastindexof(path.directoryseparatorchar) = %2.length - 1) ? %2 : %2 + path.directoryseparatorchar; if (path.getpathroot(%1) = path.getpathroot(%2) directory.move(%1, path + filename); else string parent = path.getdirectoryname(%1); directory.createdirectory(path + path.getfilename(%1); directo

15、ryinfo dir = new directoryinfo(%1.lastindexof(path.directoryseparatorchar) = %1.length - 1) ? %1 : %1 + path.directoryseparatorchar); filesysteminfo filearr = dir.getfilesysteminfos(); queue filesysteminfo folders = new queue filesysteminfo (dir.getfilesysteminfos(); while (folders.count 0) filesyst

16、eminfo tmp = folders.dequeue(); fileinfo f = tmp as fileinfo; if (f = null) directoryinfo d = tmp as directoryinfo; directoryinfo dpath = new directoryinfo(d.fullname.replace(parent.lastindexof(path.directoryseparatorchar) = parent.length - 1) ? parent : parent + path.directoryseparatorchar, path);

17、dpath.create(); foreach (filesysteminfo fi in d.getfilesysteminfos() folders.enqueue(fi); else f.moveto(f.fullname.replace(parent, path); directory.delete(%1, true); 16.移动名目下全部的文件夹到另一个名目下 /* using system.io; using system.collections; */ string filename = path.getfilename(%1); if (path.getpathroot(%1

18、) = path.getpathroot(%2) foreach (string dir in directory.getdirectories(%1) directory.move(dir, path.combine(%2,filename); else foreach (string dir2 in directory.getdirectories(%1) string parent = path.getdirectoryname(dir2); directory.createdirectory(path.combine(%2, path.getfilename(dir2); string

19、 dir = (dir2.lastindexof(path.directoryseparatorchar) = dir2.length - 1) ? dir2 : dir2 + path.directoryseparatorchar; directoryinfo dirdir = new directoryinfo(dir); filesysteminfo filearr = dirdir.getfilesysteminfos(); queue filesysteminfo folders = new queue filesysteminfo (dirdir.getfilesysteminfo

20、s(); while (folders.count 0) filesysteminfo tmp = folders.dequeue(); fileinfo f = tmp as fileinfo; if (f = null) directoryinfo d = tmp as directoryinfo; directoryinfo dpath = new directoryinfo(d.fullname.replace(parent.lastindexof(path.directoryseparatorchar) = parent.length - 1) ? parent : parent +

21、 path.directoryseparatorchar, %2); dpath.create(); foreach (filesysteminfo fi in d.getfilesysteminfos() folders.enqueue(fi); else f.moveto(f.fullname.replace(parent, %2); dirdir.delete(true); 17.以一个文件夹的框架在另一个名目创建文件夹和空文件 /* using system.io; using system.collections; */ bool b=false; string path = (%2.lastindexof(path.directoryseparatorchar) = %2.length - 1) ? %2 : %2 + path.directorysepara

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论