【发布时间】:2012-12-12 20:13:48
【问题描述】:
我已经为此苦苦挣扎了几天,无法弄清楚。
我需要从目录中最后创建的子目录中复制文件,子目录还有一些子目录可以在我找到文件之前导航,这就是问题所在。
我希望我说清楚了,我将在下面给出一个目录示例,在此先感谢您的帮助。
C:\ProgramFiles\BuildOutput\mmh\LongTerm\**49**\release\MarketMessageHandler\Service\
以粗体突出显示的数字是我需要找到最新的子目录,在服务文件夹中是我需要从中复制文件的位置...
这是我尝试过的代码
string sourceDir = @"\sttbedbsd001\BuildOutput\mmh\LongTerm\51\release\MarketMessageHandler\Service"; 字符串目标 = @"C:\Users\gwessels\Desktop\test\";
string[] sDirFiles = Directory.GetFiles(sourceDir, "*", SearchOption.TopDirectoryOnly);
string targetDir;
if (sDirFiles.Length > 0)
{
foreach (string file in sDirFiles)
{
string[] splitFile = file.Split('\\');
string copyFile = Path.GetFileName(file);
string source = sourceDir + "\\" + copyFile;
targetDir = target + copyFile;
try
{
if (File.Exists(targetDir))
{
File.Delete(targetDir);
File.Copy(source, targetDir);
}
else
{
File.Copy(source, targetDir);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
【问题讨论】:
-
定义“最新的”——你的意思是最高的数字吗?还是最近的创建日期?
-
你使用的根目录是什么?它是您存储在某处的静态目录还是您如何确定它?
-
该目录存储在本地服务器上,最新的我的意思是每次有一个新版本时都会创建一个新文件夹,其中包含所有相同的子目录 49 50 51 等
标签: c#