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;
}
}
}
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;
}
}
}
{
// strDirFrom = @"d:\tmp";
// strDirTo = @"c:\tmp";
DirectoryInfo dirInfo = new DirectoryInfo(strDirTo);
foreach(FileInfo infoFile in dirInfo.GetFiles())
{
if (File.Exists(strDirFrom + "\\" + infoFile.Name))
File.Copy(strDirFrom + "\\" + infoFile.Name,strDirTo + "\\" + infoFile.Name,true);
}
}2.在窗体中添加一个Timer,设置Enable = True,Interval为合适的时间.在Tick时间中,调用上面的函数.
MyCopy(@"d:\tmp",@"c:\tmp");
email: [email protected]
qq;20534654