C#提供的两个类Directory和DirectoryInfo分别提供静态和动态方法,但都没有clone文件夹的方法,还请高手指点
解决方案 »
- winform 注销问题
- 路径编码的的问题
- 修改datatable中数据
- System.Runtime.InteropServices.SEHException: 外部组件发生异常
- 高手,谁给发一个可以运行的C#.NET网站吧?
- 如何调用一个WinForm得到用户的选择呢?
- C# SafeNet单机版加密狗怎么做才当成网络版来用
- 求一数字正则表达式
- c#中的问题?谢谢!
- 使用VSS签入签出xml文件,xml文件变成了乱码。求救。
- 问一个if else的简单问题,在线等待
- 超级难问题,tabcontrol的alignment属性设置成Right时,tabPage的text内容怎么显示成竖着的字体!(急再线等待中。。。。)
---------------------------------------------------------------
没有现成的.
=========================
using System;
using System.IO;
namespace FrameworkExamples
{
//HOW TO: recursively copy all the files and sub dirs in a given directory
// to another directory
class SampleRecursiveCopy
{
static void Main()
{
string srcdir, destdir;
bool recursive;
recursive = true;
srcdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "images");
destdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "images2");
FileCopy(srcdir, destdir, recursive);
}
private static void FileCopy(string srcdir, string destdir, bool recursive)
{
DirectoryInfo dir;
FileInfo[] files;
DirectoryInfo[] dirs;
string tmppath;
//determine if the destination directory exists, if not create it
if (! Directory.Exists(destdir))
{
Directory.CreateDirectory(destdir);
}
dir = new DirectoryInfo(srcdir);
//if the source dir doesn't exist, throw
if (! dir.Exists)
{
throw new ArgumentException("source dir doesn't exist -> " + srcdir);
}
//get all files in the current dir
files = dir.GetFiles();
//loop through each file
foreach(FileInfo file in files)
{
//create the path to where this file should be in destdir
tmppath=Path.Combine(destdir, file.Name);
//copy file to dest dir
file.CopyTo(tmppath, false);
}
//cleanup
files = null;
//if not recursive, all work is done
if (! recursive)
{
return;
}
//otherwise, get dirs
dirs = dir.GetDirectories();
//loop through each sub directory in the current dir
foreach(DirectoryInfo subdir in dirs)
{
//create the path to the directory in destdir
tmppath = Path.Combine(destdir, subdir.Name);
//recursively call this function over and over again
//with each new dir.
FileCopy(subdir.FullName, tmppath, recursive);
}
//cleanup
dirs = null;
dir = null;
}
}
}
---------------------------------------------------------------
方法一:使用API函数
public class ShellFiles
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
[MarshalAs(UnmanagedType.U4)] public int wFunc;
public string pFrom;
public string pTo;
public short fFlags;
[MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}
[DllImport("shell32.dll", CharSet=CharSet.Auto)]
static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
const int FO_DELETE = 3;
const int FO_COPY = 2;
const int FOF_ALLOWUNDO = 0x40;
const int FOF_NOCONFIRMATION = 0x10; //Don't prompt the user.;
const int FOF_SIMPLEPROGRESS = 0x100;
public void SendToRecyclyBin(string path)
{
SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.wFunc = FO_DELETE;
shf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
shf.pFrom = path;
SHFileOperation(ref shf);
}
public void Copy(string from, string to)
{
DirectoryInfo source = new DirectoryInfo(from);
DirectoryInfo dest = new DirectoryInfo(to);
if(!dest.Exists)
dest.Create();
SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.wFunc = FO_COPY;
shf.fFlags = FOF_ALLOWUNDO;
shf.pFrom = from;
shf.pTo = to;
SHFileOperation(ref shf);
}
}
方法二:递归调用(程序简单,但比较慢)
public sealed class DirectoryUtils
{
private DirectoryUtils()
{
}
/// <summary>
/// Copies a directory to a new location.
/// </summary>
/// <param name="src">Source directory path</param>
/// <param name="dest">Destination directory path</param>
public static void CopyDirectory(String src, String dest)
{
DirectoryInfo di = new DirectoryInfo(src);
foreach(FileSystemInfo fsi in di.GetFileSystemInfos())
{
String destName = Path.Combine(dest, fsi.Name);
if (fsi is FileInfo)
File.Copy(fsi.FullName, destName);
else
{
Directory.CreateDirectory(destName);
CopyDirectory(fsi.FullName, destName);
}
}
}
}