在整理照片/视频时想根据实际拍摄时间重命名文件,但 System.IO.FileInfo 只能获取到文件的创建时间或最后写入时间,不符合要求,遂寻找解决方案
方案 1: System.Drawing
1 | static void Main(string[] args) |
此方法仅适用于图片:pass
方案 2: shell32
[How to use Shell32 within a C# application?](How to use Shell32 within a C# application?)
Exception when using Shell32 to get File extended properties
操作步骤:
添加shell32引用 (C:\Windows\System32\shell32.dll 或 在 VS 中添加引用 .COM -> Microsoft Shell Controls and Automation)
设置 dll “嵌入互操作类型” 为 false
1 | static void Main(string[] args) |
此方法适用于图片和视频(id 不同),但是在实际中获取到的时间字符串包含乱码无法转换成时间:pass
方案 3: Microsoft.WindowsAPICodePack.Shell (采用方案)
1 | static void Main(string[] args) |
此方法完全符合要求,支持所有类型的文件。同时也可以获取文件的其他信息,如作者等