2013-03-21 88 views
-1

我想制作按实际拍摄日期将照片分组为文件夹的软件。这些照片将整理与名称的文件夹采取像今年:按照拍摄日期将照片分类到文件夹

文件夹:2000

里面的文件夹:在2000年

我如何能做到这一点采取了一些照片?

回答

1
List<string> imageFiles= ... // Here you get the image path 
Dictionary<int, List<string>> groupedPaths= ... //output dict 
foreach(string str in imageFiles) 
{ 
    FileInfo fi=new FileInfo(str); 
    int year = fi.CreationTime.Year; 
    if(!groupedPath.ContainsKey(year)) 
    { 
     var list=new List<string>(); 
     list.Add(year, string); 
     groupedPaths.Add(year, list); 
    } 
    else 
    { 
     groupedPaths[year].Add(year, str); 
    } 
//Now you can process with foreach or use LINQ to group your images 
foreach(KeyValuePair<int, string> pair in groupedPaths) 
{ 
    ... 
} 
+0

感谢您的回答,但我想实际“排序”的文件作为排序到文件夹中。比如文件移动什么东西? – user2180617 2013-03-21 04:06:17

+1

您使用“排序”这个词是含糊不清的,我们无法排序,但是组合成排序是重新排序,但不是分组。 – David 2013-03-21 04:10:11

+0

我试了一下,它的工作原理,但我不熟悉字典和List类,所以我真的不知道如何将文件分组到文件夹,但感谢新的答案。 – user2180617 2013-03-22 03:19:37

1

要得到照片实际拍摄的日期,您需要查看Exif数据。

当您使用Image.FromFile()时,此数据将自动读入PropertyItems阵列。然后,您可以使用另一个参考(like this one)得到正确的代码为最新信息。你也可以使用this library方便阅读的代码。

并非所有图片都会有Exif数据中,所以你可能要纳入大卫的回答为回退。

一旦你有相关的最新信息,您可以使用Directory.Create(year)File.Move(oldPath, newPath)来组织文件。