1、C#文件操作大全上一篇 / 下一篇 2010-05-07 10:23:27 / 个人分类:.net 查看( 1535 ) / 评论( 20 ) / 评分( 79 / 1 ) 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;fore
2、ach (string dirStr in Directory.GetDirectories(%1)DirectoryInfo dir = new DirectoryInfo(dirStr);ArrayList folders=new ArrayList();FileSystemInfo fileArr = dir.GetFileSystemInfos();for (int i = 0; i Folders = new Queue(dir.GetFileSystemInfos();while (Folders.Count0)FileSystemInfo tmp = Folders.Dequeu
3、e();FileInfo f = tmp as FileInfo;if (f = null)DirectoryInfo d = tmp as DirectoryInfo;Directory.CreateDirectory(d.FullName.Replace(parent.LastIndexOf(“) = parent.Length - 1) ? parent : parent + “, path);foreach (FileSystemInfo fi in d.GetFileSystemInfos()Folders.Enqueue(fi);elsef.CopyTo(f.FullName.Re
4、place(parent, path);14.复制目录下所有的文件夹到另一个文件夹下/*using System.IO;using System.Collections;*/DirectoryInfo d = new DirectoryInfo(%1);foreach (DirectoryInfo dirs in d.GetDirectories()Queue al = new Queue(dirs.GetFileSystemInfos();while (al.Count 0)FileSystemInfo temp = al.Dequeue();FileInfo file = temp as
5、FileInfo;if (file = null)DirectoryInfo directory = temp as DirectoryInfo;Directory.CreateDirectory(path + directory.Name);foreach (FileSystemInfo fsi in directory.GetFileSystemInfos()al.Enqueue(fsi);elseFile.Copy(file.FullName, path + file.Name);15.移动文件夹/*using System.IO;using System.Collections;*/s
6、tring filename = Path.GetFileName(%1);string path=(%2.LastIndexOf(“) = %2.Length - 1) ? %2 : %2 + “;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
7、 = new DirectoryInfo(%1.LastIndexOf(“) = %1.Length - 1) ? %1 : %1 + “);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
8、 DirectoryInfo;DirectoryInfo dpath = new DirectoryInfo(d.FullName.Replace(parent.LastIndexOf(“) = parent.Length - 1) ? parent : parent + “, path);dpath.Create();foreach (FileSystemInfo fi in d.GetFileSystemInfos()Folders.Enqueue(fi);elsef.MoveTo(f.FullName.Replace(parent, path);Directory.Delete(%1,
9、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.GetDire
10、ctories(%1)string parent = Path.GetDirectoryName(dir2);Directory.CreateDirectory(Path.Combine(%2, Path.GetFileName(dir2);string dir = (dir2.LastIndexOf(“) = dir2.Length - 1) ? dir2 : dir2 + “;DirectoryInfo dirdir = new DirectoryInfo(dir);FileSystemInfo fileArr = dirdir.GetFileSystemInfos();Queue Fol
11、ders = 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.Replace(parent.LastIndexOf(“) = parent.Length - 1) ? parent :
12、 parent + “, %2);dpath.Create();foreach (FileSystemInfo fi in d.GetFileSystemInfos()Folders.Enqueue(fi);elsef.MoveTo(f.FullName.Replace(parent, %2);dirdir.Delete(true);17.以一个文件夹的框架在另一个目录创建文件夹和空文件/*using System.IO;using System.Collections;*/bool b=false;string path = (%2.LastIndexOf(“) = %2.Length -
13、1) ? %2 : %2 + “;string parent = Path.GetDirectoryName(%1);Directory.CreateDirectory(path + Path.GetFileName(%1);DirectoryInfo dir = new DirectoryInfo(%1.LastIndexOf(“) = %1.Length - 1) ? %1 : %1 + “);FileSystemInfo fileArr = dir.GetFileSystemInfos();Queue Folders = new Queue(dir.GetFileSystemInfos(
14、);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(“) = parent.Length - 1) ? parent : parent + “, path);foreach (FileSystemInfo fi in d.GetFil
15、eSystemInfos()Folders.Enqueue(fi);elseif(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(“) = %1.Length - 1) ? %1 +Path.GetFileName(fileStr): %1 +
16、 “+Path.GetFileName(fileStr),(%2.LastIndexOf(“) = %2.Length - 1) ? %2 +Path.GetFileName(fileStr): %2 + “+Path.GetFileName(fileStr);20.提取扩展名/using System.IO;string %2=Path.GetExtension(%1);21.提取文件名/using System.IO;string %2=Path.GetFileName(%1);22.提取文件路径/using System.IO;string %2=Path.GetDirectoryNam
17、e(%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+“+file.getname(%1);26.移动一个文件夹下所有文件到另一个目录foreach (string fileStr in Directory.GetFiles(%1)File.Move(%1.LastIndexOf(“) = %1.Length - 1) ? %1 +Path.Ge
18、tFileName(fileStr): %1 + “+Path.GetFileName(fileStr),(%2.LastIndexOf(“) = %2.Length - 1) ? %2 +Path.GetFileName(fileStr): %2 + “+Path.GetFileName(fileStr);27.指定目录下搜索文件/*using System.Text;using System.IO;*/string fileName=%1;string dirName=%2;DirectoryInfo dirc=new DirectoryInfo(dirName);foreach(FileInfo file in dirc.GetFiles() if(file.Name.IndexOf(fileName)-1)return file.FullName;foreach(DirectoryInfo dir in dirc.GetDirectories() return GetFile(fileName,dir.FullName); return “找不到指定的文件“;