void CopyUpdateFile(string srcPath, string aimPath) { if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar) aimPath += Path.DirectorySeparatorChar; if (!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath); if (!Directory.Exists(srcPath)) return; string[] fileList = Directory.GetFileSystemEntries(srcPath); foreach (string file in fileList) { int k = file.LastIndexOf("\\"); string filename = file.Substring(k + 1); if (filename.ToLower() != Path.GetFileName(Application.ExecutablePath).ToLower()) { if (Directory.Exists(file)) CopyUpdateFile(file, aimPath + Path.GetFileName(file)); else { try { File.Copy(file, aimPath + Path.GetFileName(file), true); //File.Delete(file); } catch (Exception e) { Console.WriteLine("复制文件失败:" + file + e.Message); continue; } } } } }
上面是我以前写的一个函数,大概意思就是这样的 int k = file.LastIndexOf("\\"); string filename = file.Substring(k + 1); if (filename.ToLower() != Path.GetFileName(Application.ExecutablePath).ToLower())把上面这几句去掉,对你没用,你不需要判断
void CopyUpdateFile(string srcPath, string aimPath)
{
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
if (!Directory.Exists(aimPath))
Directory.CreateDirectory(aimPath);
if (!Directory.Exists(srcPath))
return;
string[] fileList = Directory.GetFileSystemEntries(srcPath); foreach (string file in fileList)
{
int k = file.LastIndexOf("\\");
string filename = file.Substring(k + 1);
if (filename.ToLower() != Path.GetFileName(Application.ExecutablePath).ToLower())
{
if (Directory.Exists(file))
CopyUpdateFile(file, aimPath + Path.GetFileName(file));
else
{
try
{
File.Copy(file, aimPath + Path.GetFileName(file), true);
//File.Delete(file);
}
catch (Exception e)
{
Console.WriteLine("复制文件失败:" + file + e.Message);
continue;
}
}
}
}
}
string filename = file.Substring(k + 1);
if (filename.ToLower() != Path.GetFileName(Application.ExecutablePath).ToLower())把上面这几句去掉,对你没用,你不需要判断
善用baidu
http://www.cnblogs.com/c-delight/archive/2005/10/09/251126.html