C#文件操作大全.docx

上传人:b****6 文档编号:7613426 上传时间:2023-01-25 格式:DOCX 页数:86 大小:53.25KB
下载 相关 举报
C#文件操作大全.docx_第1页
第1页 / 共86页
C#文件操作大全.docx_第2页
第2页 / 共86页
C#文件操作大全.docx_第3页
第3页 / 共86页
C#文件操作大全.docx_第4页
第4页 / 共86页
C#文件操作大全.docx_第5页
第5页 / 共86页
点击查看更多>>
下载资源
资源描述

C#文件操作大全.docx

《C#文件操作大全.docx》由会员分享,可在线阅读,更多相关《C#文件操作大全.docx(86页珍藏版)》请在冰豆网上搜索。

C#文件操作大全.docx

C#文件操作大全

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.删除一个目录下所有的文件夹

//usingSystem.IO;

foreach(stringdirStrinDirectory.GetDirectories(%%1))

{

DirectoryInfodir=newDirectoryInfo(dirStr);

ArrayListfolders=newArrayList();

FileSystemInfo[]fileArr=dir.GetFileSystemInfos();

for(inti=0;i

{

FileInfof=folders[i]asFileInfo;

if(f==null)

{

DirectoryInfod=folders[i]asDirectoryInfo;

d.Delete();

}

}

}

6.清空文件夹

//usingSystem.IO;

Directory.Delete(%%1,true);

Directory.CreateDirectory(%%1);

7.读取文件

7.1.操作系统默认编码

//usingSystem.IO;

StreamReaders=File.OpenText(%%1);

string%%2=null;

while((%%2=s.ReadLine())!

=null){

%%3

}

s.Close();

7.2.UTF-8编码

/*

usingSystem.IO;

usingSystem.Text;

*/

StreamReadersrfile=newStreamReader(%%1,Encoding.UTF8);

while((String%%2=srfile.ReadLine())!

=null)

{

%%3

}

srfile.Close();

7.3.分块读取

/*

usingSystem.IO;

usingSystem.Text;

*/

try

{

FileStreamfs=newFileStream(%%1,FileMode.Open,FileAccess.Read);

BinaryReaderbr=newBinaryReader(fs,newASCIIEncoding());

byte[]chunk;

do

{

chunk=br.ReadBytes(10240);

if(chunk.Length>0)

{

%%2//chunk,chunk.Length

}

}

while(chunk.Length>0);

fs.Close();

}

catch

{

//return-1;

}

8.写入文件

//usingSystem.IO;

FileInfof=newFileInfo(%%1);

StreamWriterw=f.CreateText();

w.WriteLine(%%2);

w.Flush();

w.Close();

9.写入随机文件

//usingSystem.IO;

byte[]dataArray=newbyte[100000];//newRandom().NextBytes(dataArray);

using(FileStreamFileStream=newFileStream(%%1,FileMode.Create)){

//Writethedatatothefile,bytebybyte.

for(inti=0;i

FileStream.WriteByte(dataArray[i]);

}

//Setthestreampositiontothebeginningofthefile.

FileStream.Seek(0,SeekOrigin.Begin);

//Readandverifythedata.

for(inti=0;i

if(dataArray[i]!

=FileStream.ReadByte()){

//写入数据错误

return;

}

}

//"数据流"+FileStream.Name+"已验证"

}

10.读取文件属性

//usingSystem.IO;

FileInfof=newFileInfo(%%1);//f.CreationTime,f.FullName

if((f.Attributes&FileAttributes.ReadOnly)!

=0){

%%2

}

else{

%%3

}

11.写入属性

//usingSystem.IO;

FileInfof=newFileInfo(%%1);

//设置只读

f.Attributes=myFile.Attributes|FileAttributes.ReadOnly;

//设置可写

f.Attributes=myFile.Attributes&~FileAttributes.ReadOnly;

12.枚举一个文件夹中的所有文件夹

//usingSystem.IO;

foreach(string%%2inDirectory.GetDirectories(%%1)){

%%3

}

/*

DirectoryInfodir=newDirectoryInfo(%%1);

FileInfo[]files=dir.GetFiles("*.*");

foreach(FileInfo%%2infiles){

%%3

}

*/

13.复制文件夹

/*

usingSystem.IO;

usingSystem.Collections;

*/

stringpath=(%%2.LastIndexOf(Path.DirectorySeparatorChar)==%%2.Length-1)?

%%2:

%%2+Path.DirectorySeparatorChar;

stringparent=Path.GetDirectoryName(%%1);

Directory.CreateDirectory(path+Path.GetFileName(%%1));

DirectoryInfodir=newDirectoryInfo((%%1.LastIndexOf(Path.DirectorySeparatorChar)==%%1.Length-1)?

%%1:

%%1+Path.DirectorySeparatorChar);

FileSystemInfo[]fileArr=dir.GetFileSystemInfos();

QueueFolders=newQueue(dir.GetFileSystemInfos());

while(Folders.Count>0)

{

FileSystemInfotmp=Folders.Dequeue();

FileInfof=tmpasFileInfo;

if(f==null)

{

DirectoryInfod=tmpasDirectoryInfo;

Directory.CreateDirectory(d.FullName.Replace((parent.LastIndexOf(Path.DirectorySeparatorChar)==parent.Length-1)?

parent:

parent+Path.DirectorySeparatorChar,path));

foreach(FileSystemInfofiind.GetFileSystemInfos())

{

Folders.Enqueue(fi);

}

}

else

{

f.CopyTo(f.FullName.Replace(parent,path));

}

}

14.复制目录下所有的文件夹到另一个文件夹下

/*

usingSystem.IO;

usingSystem.Collections;

*/

DirectoryInfod=newDirectoryInfo(%%1);

foreach(DirectoryInfodirsind.GetDirectories())

{

Queueal=newQueue(dirs.GetFileSystemInfos());

while(al.Count>0)

{

FileSystemInfotemp=al.Dequeue();

FileInfofile=tempasFileInfo;

if(file==null)

{

DirectoryInfodirectory=tempasDirectoryInfo;

Directory.CreateDirectory(path+directory.Name);

foreach(FileSystemInfofsiindirectory.GetFileSystemInfos())

al.Enqueue(fsi);

}

else

File.Copy(file.FullName,path+file.Name);

}

}

15.移动文件夹

/*

usingSystem.IO;

usingSystem.Collections;

*/

stringfilename=Path.GetFileName(%%1);

stringpath=(%%2.LastIndexOf(Path.DirectorySeparatorChar)==%%2.Length-1)?

%%2:

%%2+Path.DirectorySeparatorChar;

if(Path.GetPathRoot(%%1)==Path.GetPathRoot(%%2))

Directory.Move(%%1,path+filename);

else

{

stringparent=Path.GetDirectoryName(%%1);

Directory.CreateDirectory(path+Path.GetFileName(%%1));

DirectoryInfodir=newDirectoryInfo((%%1.LastIndexOf(Path.DirectorySeparatorChar)==%%1.Length-1)?

%%1:

%%1+Path.DirectorySeparatorChar);

FileSystemInfo[]fileArr=dir.GetFileSystemInfos();

QueueFolders=newQueue(dir.GetFileSystemInfos());

while(Folders.Count>0)

{

FileSystemInfotmp=Folders.Dequeue();

FileInfof=tmpasFileInfo;

if(f==null)

{

DirectoryInfod=tmpasDirectoryInfo;

DirectoryInfodpath=newDirectoryInfo(d.FullName.Replace((parent.LastIndexOf(Path.DirectorySeparatorChar)==parent.Length-1)?

parent:

parent+Path.DirectorySeparatorChar,path));

dpath.Create();

foreach(FileSystemInfofiind.GetFileSystemInfos())

{

Folders.Enqueue(fi);

}

}

else

{

f.MoveTo(f.FullName.Replace(parent,path));

}

}

Directory.Delete(%%1,true);

}

16.移动目录下所有的文件夹到另一个目录下

/*

usingSystem.IO;

usingSystem.Collections;

*/

stringfilename=Path.GetFileName(%%1);

if(Path.GetPathRoot(%%1)==Path.GetPathRoot(%%2))

foreach(stringdirinDirectory.GetDirectories(%%1))

Directory.Move(dir,Path.Combine(%%2,filename));

else

{

foreach(stringdir2inDirectory.GetDirectories(%%1))

{

stringparent=Path.GetDirectoryName(dir2);

Directory.CreateDirectory(Path.Combine(%%2,Path.GetFileName(dir2)));

stringdir=(dir2.LastIndexOf(Path.DirectorySeparatorChar)==dir2.Length-1)?

dir2:

dir2+Path.DirectorySeparatorChar;

DirectoryInfodirdir=newDirectoryInfo(dir);

FileSystemInfo[]fileArr=dirdir.GetFileSystemInfos();

QueueFolders=newQueue(dirdir.GetFileSystemInfos());

while(Folders.Count>0)

{

FileSystemInfotmp=Folders.Dequeue();

FileInfof=tmpasFileInfo;

if(f==null)

{

DirectoryInfod=tmpasDirectoryInfo;

DirectoryInfodpath=newDirectoryInfo(d.FullName.Replace((parent.LastIndexOf(Path.DirectorySeparatorChar)==parent.Length-1)?

parent:

parent+Path.DirectorySeparatorChar,%%2));

dpath.Create();

foreach(FileSystemInfofiind.GetFileSystemInfos())

{

Folders.Enqueue(fi);

}

}

else

{

f.MoveTo(f.FullName.Replace(parent,%%2));

}

}

dirdir.Delete(true);

}

}

17.以一个文件夹的框架在另一个目录创建文件夹和空文件

/*

usingSystem.IO;

usingSystem.Collections;

*/

boolb=false;

stringpath=(%%2.LastIndexOf(Path.DirectorySeparatorChar)==%%2.Length-1)?

%%2:

%%2+Path.DirectorySeparatorChar;

stringparent=Path.GetDirectoryName(%%1);

Directory.CreateDirectory(path+Path.GetFileName(%%1));

DirectoryInfodir=newDirectoryInfo((%%1.LastIndexOf(Path.DirectorySeparatorChar)==%%1.Length-1)?

%%1:

%%1+Path.DirectorySeparatorChar);

FileSystemInfo[]fileArr=dir.GetFileSystemInfos();

QueueFolders=newQueue(dir.GetFileSystemInfos());

while(Folders.Count>0)

{

FileSystemInfotmp=Folders.Dequeue();

FileInfof=tmpasFileInfo;

if(f==null)

{

DirectoryInfod=tmpasDirectoryInfo;

Directory.CreateDirectory(d.FullName.Replace((parent.LastIndexOf(Path.DirectorySeparatorChar)==parent.Length-1)?

parent:

parent+Path.DirectorySeparatorChar,path));

foreach(FileSystemInfofiind.GetFileSystemInfos())

{

Folders.Enqueue(fi);

}

}

else

{

if(b)File.Create(f.FullName.Replace(parent,path));

}

}

18.复制文件

//usingSystem.IO;

File.Copy(%%1,%%2);

19.复制一个文件夹下所有的文件到另一个目录

//usingSystem.IO;

foreach(stringfileStrinDirectory.GetFiles(%%1))

File.Copy((%%1.LastIndexOf(Path.DirectorySeparatorChar)==%%1.Length-1)?

%%1+Path.GetFileName(fileStr):

%%1+Path.DirectorySeparatorChar+Path.GetFileName(fileStr),(%%2.LastIndexOf(Path.DirectorySeparatorChar)==%%2.Length-1)?

%%2+Path.GetFileName(fileStr):

%%2+Path.DirectorySeparatorChar+Path.GetFileName(fileStr));

20.提取扩展名

//usingSystem.IO;

string%%2=Path.GetExtension(%%1);

21.提取文件名

//usingSystem.IO;

string%%2=Path.GetFileName(%%1);

22.提取文件路径

//usingSystem.IO;

string%%2=Path.GetDirectoryName(%%1);

23.替换扩展名

//usingSystem.IO;

File.ChangeExtension(%%1,%%2);

24.追加路径

//usingSystem.IO;

string%%3=Path.Combine(%%1,%%2);

25.移动文件

//usingSystem.IO;

File.Move(%%1,%%2+Path.DirectorySeparatorChar+file.getname(%%1));

26.移动一个文件夹下所有文件到另一个目录

//usingSystem.IO;

foreach(stringfileStrinDirectory.GetFiles(%%1))

File.Move((%%1.LastIndexOf(Path.DirectorySeparatorChar)==%%1.Length-1)?

%%1+Path.GetFileName(fileStr):

%%1+Path.DirectorySeparatorChar+Path.GetFileName(fileStr),(%%2.LastIndexOf(Path.DirectorySeparatorChar)==%%2.Length-1)?

%%2+Path.GetFileName(fileStr):

%%2+Path.DirectorySeparatorChar+Path.GetFileName(fileStr));

27.指定目录下搜索文件

/*

usingSystem.Text;

usingSystem.IO;

*/

stringfileName=%%1;

DriveInfo[]drives=DriveInfo.GetDrives();

stringparent=Path.GetDirectoryName(%%2);

DirectoryInfodir=newDirectoryInfo((%%2.LastIndexOf(Path.DirectorySeparatorChar)==%%2.Length-1)?

%%2:

%%2+Path.DirectorySeparatorChar);

FileSystemInfo[]fileArr;

try

{

fileArr=dir.GetFileSystemInfos();

}

catch(Exception)

{

continue;

}

QueueFolders=newQueue(dir.GetF

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1