提问人:Mateusz Kubis 提问时间:11/10/2023 最后编辑:CompoMateusz Kubis 更新时间:11/10/2023 访问量:44
C# 将文件夹缩略图设置为该目录中的最新照片
C# set folder thumbnail to newest photo in that catalog
问:
有没有办法根据 c# 代码自动刷新目录缩略图?
我有大量包含照片和文本文件的目录,我想快速将目录缩略图更新为最新照片(始终为 .jpg)
我尝试过使用 ChatGPT,但在某些时候它取决于 dll 或函数。
我最接近下面的代码,但显然不存在......folderItem.InvokeVerb("setthumbnail")
有人尝试使用代码或脚本更改文件夹缩略图吗? 我很迷茫,这将对我有很大的帮助。
提前致谢 o7
class Program
{
static void Main()
{
string startPath = @"D:\Test\1000";
string[] folders = Directory.GetDirectories(startPath);
foreach (string folder in folders)
{
string[] jpgFiles = Directory.GetFiles(folder, "*.jpg");
Array.Sort(jpgFiles, (a, b) => new FileInfo(b).LastWriteTime.CompareTo(new FileInfo(a).LastWriteTime));
if (jpgFiles.Length > 0)
{
string imagePath = jpgFiles[0];
SetFolderThumbnail(folder, imagePath);
}
}
}
static void SetFolderThumbnail(string folderPath, string imagePath)
{
try
{
DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
FileInfo imageFileInfo = new FileInfo(imagePath);
string thumbsDbPath = Path.Combine(folderPath, "Thumbs.db");
if (!File.Exists(thumbsDbPath))
{
File.Create(thumbsDbPath).Close();
}
Shell shell = new Shell();
Folder folder = GetShell32Folder(folderPath);
FolderItem folderItem = folder.ParseName(dirInfo.Name);
FolderItem imageItem = folder.ParseName(imageFileInfo.Name);
folderItem.InvokeVerb("setthumbnail");
imageItem.InvokeVerb("setthumbnail");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private static Shell32.Folder GetShell32Folder(string folderPath)
{
Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
Object shell = Activator.CreateInstance(shellAppType);
return (Shell32.Folder)shellAppType.InvokeMember("NameSpace",
System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { folderPath });
}
[ComImport]
[Guid("13709620-C279-11CE-A49E-444553540000")]
public class Shell
{
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214E6-0000-0000-C000-000000000046")]
public interface IShellFolder
{
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("93F2F68C-1D1B-11D3-A30E-00C04F79ABD1")]
public interface IFolderView
{
}
}
答: 暂无答案
评论