C#文件操作大全_第1頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

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.操作系統(tǒng)默認(rèn)編碼 /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.寫入隨機(jī)文件 /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() /寫入數(shù)據(jù)錯誤 return; / 數(shù)據(jù)流 +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); /設(shè)置只讀 f.attributes = myfile.attributes | fileattributes.readonly; /設(shè)置可寫 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.復(fù)制文件夾 /* 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.復(fù)制名目下全部的文件夾到另一個文件夾下 /* 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.以一個文件夾的框架在另一個名目創(chuàng)建文件夾和空文件 /* 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論