ImageVerifierCode 换一换
格式:DOCX , 页数:86 ,大小:53.25KB ,
资源ID:7613426      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7613426.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C#文件操作大全.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C#文件操作大全.docx

1、C#文件操作大全1.创建文件夹/using System.IO;Directory.CreateDirectory(%1);2.创建文件/using System.IO;File.Create(%1);3.删除文件/using System.IO;File.Delete(%1);4.删除文件夹/using System.IO;Directory.Delete(%1);5.删除一个目录下所有的文件夹/using System.IO;foreach (string dirStr in Directory.GetDirectories(%1)DirectoryInfo dir = new Direc

2、toryInfo(dirStr);ArrayList folders=new ArrayList();FileSystemInfo fileArr = dir.GetFileSystemInfos();for (int i = 0; i 0)%2 /chunk,chunk.Lengthwhile(chunk.Length0);fs.Close();catch/return -1;8.写入文件/using System.IO;FileInfo f = new FileInfo(%1);StreamWriter w = f.CreateText();w.WriteLine(%2);w.Flush(

3、);w.Close();9.写入随机文件/using System.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

4、position to the beginning of 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.FullNamei

5、f(f.Attributes & FileAttributes.ReadOnly) != 0)%2else%311.写入属性/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 Directo

6、ry.GetDirectories(%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.DirectorySeparatorCha

7、r;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();Queue Folder

8、s = new Queue(dir.GetFileSystemInfos();while (Folders.Count0)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.DirectorySeparatorChar) = parent.Length - 1) ? par

9、ent : parent + Path.DirectorySeparatorChar, path);foreach (FileSystemInfo fi in d.GetFileSystemInfos()Folders.Enqueue(fi);elsef.CopyTo(f.FullName.Replace(parent, path);14.复制目录下所有的文件夹到另一个文件夹下/*using System.IO;using System.Collections;*/DirectoryInfo d = new DirectoryInfo(%1);foreach (DirectoryInfo di

10、rs in d.GetDirectories()Queue al = new Queue(dirs.GetFileSystemInfos();while (al.Count 0)FileSystemInfo temp = al.Dequeue();FileInfo file = temp as FileInfo;if (file = null)DirectoryInfo directory = temp as DirectoryInfo;Directory.CreateDirectory(path + directory.Name);foreach (FileSystemInfo fsi in

11、 directory.GetFileSystemInfos()al.Enqueue(fsi);elseFile.Copy(file.FullName, path + file.Name);15.移动文件夹/*using System.IO;using System.Collections;*/string filename = Path.GetFileName(%1);string path=(%2.LastIndexOf(Path.DirectorySeparatorChar) = %2.Length - 1) ? %2 : %2 + Path.DirectorySeparatorChar;

12、if (Path.GetPathRoot(%1) = Path.GetPathRoot(%2)Directory.Move(%1, path + filename);elsestring parent = Path.GetDirectoryName(%1);Directory.CreateDirectory(path + Path.GetFileName(%1);DirectoryInfo dir = new DirectoryInfo(%1.LastIndexOf(Path.DirectorySeparatorChar) = %1.Length - 1) ? %1 : %1 + Path.D

13、irectorySeparatorChar);FileSystemInfo fileArr = dir.GetFileSystemInfos();Queue Folders = new Queue(dir.GetFileSystemInfos();while (Folders.Count 0)FileSystemInfo tmp = Folders.Dequeue();FileInfo f = tmp as FileInfo;if (f = null)DirectoryInfo d = tmp as DirectoryInfo;DirectoryInfo dpath = new Directo

14、ryInfo(d.FullName.Replace(parent.LastIndexOf(Path.DirectorySeparatorChar) = parent.Length - 1) ? parent : parent + Path.DirectorySeparatorChar, path);dpath.Create();foreach (FileSystemInfo fi in d.GetFileSystemInfos()Folders.Enqueue(fi);elsef.MoveTo(f.FullName.Replace(parent, path);Directory.Delete(

15、%1, true);16.移动目录下所有的文件夹到另一个目录下/*using System.IO;using System.Collections;*/string filename = Path.GetFileName(%1);if (Path.GetPathRoot(%1) = Path.GetPathRoot(%2)foreach (string dir in Directory.GetDirectories(%1)Directory.Move(dir, Path.Combine(%2,filename);elseforeach (string dir2 in Directory.Get

16、Directories(%1)string parent = Path.GetDirectoryName(dir2);Directory.CreateDirectory(Path.Combine(%2, Path.GetFileName(dir2);string dir = (dir2.LastIndexOf(Path.DirectorySeparatorChar) = dir2.Length - 1) ? dir2 : dir2 + Path.DirectorySeparatorChar;DirectoryInfo dirdir = new DirectoryInfo(dir);FileSy

17、stemInfo fileArr = dirdir.GetFileSystemInfos();Queue Folders = new Queue(dirdir.GetFileSystemInfos();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.Repla

18、ce(parent.LastIndexOf(Path.DirectorySeparatorChar) = parent.Length - 1) ? parent : parent + Path.DirectorySeparatorChar, %2);dpath.Create();foreach (FileSystemInfo fi in d.GetFileSystemInfos()Folders.Enqueue(fi);elsef.MoveTo(f.FullName.Replace(parent, %2);dirdir.Delete(true);17.以一个文件夹的框架在另一个目录创建文件夹和

19、空文件/*using System.IO;using System.Collections;*/bool b=false;string path = (%2.LastIndexOf(Path.DirectorySeparatorChar) = %2.Length - 1) ? %2 : %2 + Path.DirectorySeparatorChar;string parent = Path.GetDirectoryName(%1);Directory.CreateDirectory(path + Path.GetFileName(%1);DirectoryInfo dir = new Dir

20、ectoryInfo(%1.LastIndexOf(Path.DirectorySeparatorChar) = %1.Length - 1) ? %1 : %1 + Path.DirectorySeparatorChar);FileSystemInfo fileArr = dir.GetFileSystemInfos();Queue Folders = new Queue(dir.GetFileSystemInfos();while (Folders.Count 0)FileSystemInfo tmp = Folders.Dequeue();FileInfo f = tmp as File

21、Info;if (f = null)DirectoryInfo d = tmp as DirectoryInfo;Directory.CreateDirectory(d.FullName.Replace(parent.LastIndexOf(Path.DirectorySeparatorChar) = parent.Length - 1) ? parent : parent + Path.DirectorySeparatorChar, path);foreach (FileSystemInfo fi in d.GetFileSystemInfos()Folders.Enqueue(fi);el

22、seif(b) File.Create(f.FullName.Replace(parent, path);18.复制文件/using System.IO;File.Copy(%1,%2);19.复制一个文件夹下所有的文件到另一个目录/using System.IO;foreach (string fileStr in Directory.GetFiles(%1)File.Copy(%1.LastIndexOf(Path.DirectorySeparatorChar) = %1.Length - 1) ? %1 +Path.GetFileName(fileStr): %1 + Path.Dire

23、ctorySeparatorChar+Path.GetFileName(fileStr),(%2.LastIndexOf(Path.DirectorySeparatorChar) = %2.Length - 1) ? %2 +Path.GetFileName(fileStr): %2 + Path.DirectorySeparatorChar+Path.GetFileName(fileStr);20.提取扩展名/using System.IO;string %2=Path.GetExtension(%1);21.提取文件名/using System.IO;string %2=Path.GetF

24、ileName(%1);22.提取文件路径/using System.IO;string %2=Path.GetDirectoryName(%1);23.替换扩展名/using System.IO;File.ChangeExtension(%1,%2);24.追加路径/using System.IO;string %3=Path.Combine(%1,%2);25.移动文件/using System.IO;File.Move(%1,%2+Path.DirectorySeparatorChar+file.getname(%1);26.移动一个文件夹下所有文件到另一个目录/using System

25、.IO;foreach (string fileStr in Directory.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

26、(fileStr): %2 + Path.DirectorySeparatorChar+Path.GetFileName(fileStr);27.指定目录下搜索文件/*using System.Text;using System.IO;*/string fileName = %1;DriveInfo drives = DriveInfo.GetDrives();string parent = Path.GetDirectoryName(%2);DirectoryInfo dir = new DirectoryInfo(%2.LastIndexOf(Path.DirectorySeparatorChar) = %2.Length - 1) ? %2 : %2 + Path.DirectorySeparatorChar);FileSystemInfo fileArr;tryfileArr = dir.GetFileSystemInfos();catch (Exception)continue;Queue Folders = new Queue(dir.GetF

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

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