C#对文件的各种操作方法

所需积分/C币:9 2013-03-12 18:40:37 4.67MB PDF
0
收藏 收藏
举报

使用C#语言对文件的各种操作,不一定是最全的,但希望有你所需要的。
10.读取文件属性 FileInfo f= new FileInfo(path): //f Creation Time, f. FullName if(.Attributes FileAttributes ReadOnly)=0) %%2 elset %%3 11.写入属性 FileInfo f= new FileInfo(path); ∥设置只读 f Attributes my File Attributes I FileAttributes. Readonly: ∥设置可写 f Attributes my File Attributes&File Attributes. Readonly 12枚举一个文件夹中的所有文件夹 foreach(string %%2 in Directory. GetDirectories(path)) %%3 /DirectoryInfo dir new DirectoryInfo(path) Filelnfol files= dir. GetFiles(" "" foreach(Filelnfo %%2 in files)(%%3*/ 13复制文件夹 using System IO; using System. Collections string path =(%%2.LastIndexof(l)==%%2.Length-1)? %%02: %%2+1; string parent= Path. GetDirectory Name(path) Directory. CreateDirectory(path Path GetFileName(path)) DirectoryInfo dir new DirectoryInfo((path. LastIndexof(1)==path Length-1)? path: path +1") File SystemInfo fileArr= dir. Get File SystemInfos( Queue<File SystemInfo> Folders= new Queue<File SystemInfo>(dir. GetFile SystemInfos() while( Folders. Count>0) File SystemInfo tmp= Folders. Dequeue() FileInfo f= tmp as FileInfo (f== null) Directorylnfo d= tmp as Directorylnfo Directory. Create Directory(d. FullName Replace((parent LastIndexOf("")== parent Length-1)? parent: parent +"l, path)); foreach( File SystemInfo fi in d. GetFile SystemInfos() Folders. Enqueue(fi) else f Copy To(f. FulIName Replace(parent, path)): 14复制目录下所有的文件夹到另一个文件夹下 using System. IO; using System. Collections: DirectoryInfo d= new DirectoryInfo(path) foreach(Directorylnfo dirs in d. GetDirectories() Queue<File SystemInfo> al new Queue<File SystemInfo>(dirs. GetFile SystemInfos(o) while(al. Count >0) File SystemInfo temp= al Dequeue FileInfo file temp as FileInfo if (file== null) DirectoryInfo directory temp as DirectoryInfo Directory. Create Directory(path+ directory Name): foreach(File SystemInfo 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(path); string path=(%%2. LastIndexOf(\)==%%2. Length-1)? %%2: %%2+N if(Path GetPath Root(path)== Path GetPath Root(%%2)) Directory. Move(path, path + filename) else string parent= Path. GetDirectory Name(path) Directory. Create Directory(path Path GetFileName(path)) DirectoryInfo dir new DirectoryInfo(path LastIndexOf("1")== path Length-1)? path: path+1") File SystemInfo fileArr= dir. Get File SystemInfos( Queue<File SystemInfo> Folders new Queue<File SystemInfo>(dir. GetFile SystemInfosO while(Folders. Count >0) File SystemInfo tmp= Folders. Dequeue 0; FileInfo f= tmp as FileInfo (f = null) Directorylnfo d= tmp as Directorylnfo: DirectoryInfo dpath new DirectoryInfo(d. FullName Replace((parent LastindexOf(\")== parent Length 1)? parent: parent 11, path)); dpath Create() foreach(File SystemInfo fi in d. GetFile SystemInfos( Folders. Enqueue(fi); else f Move To(f. FullName Replaceparent, path) Directory Delete(path, true) 16移动日录下所有的文件夹到另一个目录下 using System. IO; using System. Collections; tring filename= Path GetF Name(path); if(Path GetPath Root(path)== Path GetPath Root(%%2)) foreach(string dir in Directory. Get Directories(path)) Directory. Move(dir, Path. Combine(%%2, filename)); else foreach(string dir2 in Directory. GetDirectories(path) string parent= Path. GetDirectoryName(dir2): Directory. Create Directory(Path. Combine(%%2, Path. GetFileName( dir2) string dir=(dir2. LastIndexof(11)== dir2. Length-1)? dir2: dir2+"l Directorylnfo dirdir new DirectoryInfo(dir) File SystemInfo fileArr dirdir GetFile SystemInfos(; QueueFile SystemInfo> Folders= new Queue<File SystemInfo>(dirdir. GetFile SystemInfos()) while( Folders. Count >0) FileSystemInfo tmp= Folders. Dequeue(: FileInfo f= tmp as FileInfo if(f== null) Directorylnfo d= tmp as Directorylnfo: DirectoryInfo dpath new DirectoryInfo(d. FullName Replace((parent LastIndexOf(1t)== parent Length 1)? parent: parent +",%%2)) dpath Create O foreach( File SystemInfo fi in d. GetFile SystemInfos( Folders. Enqueue(fi) else f Move To(f. FullName Replace(parent, %%2)) dirdir Delete(true) 17以一个文件夹的框架在另一个目录创建文件夹和空文件 using System. IO; using System. Collections bool b=false string path=(%%2.Lastindexof("")==%%2.Length-1)?%%2: %%2+"I string parent= Path. GetDirectoryName(path) Directory. Create Directory(path Path GetFileName(path)); Directorylnfo dir new DirectoryInfo((path LastIndexOf(1t)==path Length-1)? path: path+W"); File SystemInfo file Arr=dir. Get File SystemInfos( QueueFile SystemInfo> Folders= new Queue<File SystemInfo>(dir. GetFile SystemInfoso): while(Folders. Count >0) File SystemInfo tmp= Folders. Dequeue(; FileInfo f= tmp as FileInfo (f if(f==nu Directorylnfo d=tmp as DirectoryInfo; Directory. Create Directory(d. FullName Replace((parent LastIndexof(\")== parent Length-1)? parent parent +l, path)) foreach(File SystemInfo fi in d. GetFileSystemInfos() Folders. Enqueue(fi); else if(b) File. Create(f. FullName Replace(parent, path)); 18复制文件 File Copy(path, %%2); 19.复制一个文件夹下所有的文件到另一个目录 foreach(string file Str in Directory. GetFiles(path) File Copy((path LastIndexof(1)== path Length -1)? path + Path. GetFileName(file Str): path ""+Path. GetFileName (fileStr) (%%2 Lastindexof("1")==%%2.Length-1)?%%2 +Path. GetFileName (file Str): %%2+"I"+Path. GetFileName(file Str)) 20.提取扩展名 string %%2=Path. GetExtension(path) 21提取文件名 string %%2=Path. GetFileName(path) 22提取文件路径 string %%2=Path. GetDirectory Name(path) 23替换扩展名 File. Change Extension(path, %%2 24追加路径 string %%3=Path. Combine(path, %%2): 25移动文件 File Move(path, %%2+"\+file getname(path) 26移动一个文件夹下所有文件到另一个目录 foreach(string file Str in Directory. GetFiles(path) File Move((path LastIndexof("l)== path Length-1)? path +Path. GetFileName(file Str): path ""+Path. GetFileName(file Str), (%%2 LastIndexof("\")==%%2.Length-1)?%%2 +Path. GetFileName (file Str): %%2+"ll+Path. GetFileName(file Str) 27指定目录下搜索文件 using System. Text: using System. IO; string fileName=path; string dirName=%%2: Directorylnfo dirc=new DirectoryInfo(dirName); foreach(FileInfo file in dirc GetFiles() if(file Name. Index Of(fileName)>-1) return file. FullName: foreach(DirectoryInfo dir in dirc. GetDirectories()( return GetFile(fileName, dir. FullName) return"找不到指定的文件"; 28打开对话框 Open File Dialog pen File Dialog=new Open File Dialog() open File Dialog. Initial Directory="C:N;∥/注意这里写路径时要用c:而不是c openFileDialog Filter=文本文件*C#文件”cs所有文件"; open File Dialog. Restore Directory=true open FileDialog. FilterIndex=1; if (openFile Dialog Show Dialog()==Dialog Result. OK)I fName=openFileDialog FileName: File fileOpen=new File(fName) isFileHaveName=true path=fileOpen ReadFile( ath. Append Text("\); 29文件分割 File Stream fsr= new FileStream(path, File Mode. Open, File Access. Read); byte[ btArr= new byte[fsr Length fsr. Read(btArr, 0, btArr. Length) fsr. Close ( string strFileName=path Substring(path LastIndexof(l)+1) File Stream fsw= new File Stream(%%2+strFileName +1",FileMode Create, FileAccess. Write) fsw Write(btArr, 0, btArr Length/2)

...展开详情
试读 127P C#对文件的各种操作方法
立即下载 低至0.43元/次 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
兵强马壮2018 比较齐全的对文件操作的方法,学习了!
2013-03-14
回复
上传资源赚积分or赚钱
    最新推荐
    C#对文件的各种操作方法 9积分/C币 立即下载
    1/127
    C#对文件的各种操作方法第1页
    C#对文件的各种操作方法第2页
    C#对文件的各种操作方法第3页
    C#对文件的各种操作方法第4页
    C#对文件的各种操作方法第5页
    C#对文件的各种操作方法第6页
    C#对文件的各种操作方法第7页
    C#对文件的各种操作方法第8页
    C#对文件的各种操作方法第9页
    C#对文件的各种操作方法第10页
    C#对文件的各种操作方法第11页
    C#对文件的各种操作方法第12页
    C#对文件的各种操作方法第13页
    C#对文件的各种操作方法第14页
    C#对文件的各种操作方法第15页
    C#对文件的各种操作方法第16页
    C#对文件的各种操作方法第17页
    C#对文件的各种操作方法第18页
    C#对文件的各种操作方法第19页
    C#对文件的各种操作方法第20页

    试读结束, 可继续阅读

    9积分/C币 立即下载 >