123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- using System;
- using System.IO;
- namespace Fort23.Core
- {
- public class FileHelper
- {
- public static void CleanDirectory(string dir)
- {
- foreach (string subdir in Directory.GetDirectories(dir))
- {
- Directory.Delete(subdir, true);
- }
- foreach (string subFile in Directory.GetFiles(dir))
- {
- File.Delete(subFile);
- }
- }
- public static void CopyDirectory(string srcDir, string tarTgtDir)
- {
- DirectoryInfo source = new DirectoryInfo(srcDir);
- DirectoryInfo target = new DirectoryInfo(tarTgtDir);
- if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
- {
- throw new Exception("父目录不能拷贝到子目录!");
- }
- if (!source.Exists)
- return;
- if (!target.Exists)
- target.Create();
- FileInfo[] files = source.GetFiles();
- for (int i = 0; i < files.Length; i++)
- {
- File.Copy(files[i].FullName, Path.Combine(target.FullName, files[i].Name), true);
- }
- DirectoryInfo[] dirs = source.GetDirectories();
- for (int j = 0; j < dirs.Length; j++)
- {
- CopyDirectory(dirs[j].FullName, Path.Combine(target.FullName, dirs[j].Name));
- }
- }
- public static void CopyFile(string srcPath, string targetPath)
- {
- File.WriteAllBytes(targetPath, File.ReadAllBytes(srcPath));
- }
- }
- }
|