Hi guys i am still new in C#, i have files in a directory in c:\SourceFolder which are concatenated with a date string in the format FileName_YYYYMMDD looking like this:
report_20130220.text,
report_20130222.text,
report_20130228.text,
i want to copy only the file with the maximum date e.g(report_20130228.text) to another directory, please see the code but it copies all the files what am i not doing right?
class Program
{
static void Main(string[] args)
{
Program copy = new Program();
DirectoryInfo sourcedinfo = new DirectoryInfo(@"C:\Users\Input");
DirectoryInfo destinfo = new DirectoryInfo(@"C:\Users\Output");
copy.CopyAll(sourcedinfo, destinfo);
Console.Read();
}
public void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
try
{
//check if the target directory exists
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
//copy all the files into the new directory
foreach (FileInfo fi in source.GetFiles())
{
Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
Console.WriteLine("Success");
}
catch (IOException ie)
{
Console.WriteLine(ie.Message);
}
}
}