gpt4 book ai didi

C# 如何设置文件夹图标?

转载 作者:太空宇宙 更新时间:2023-11-03 14:16:12 28 4
gpt4 key购买 nike

我使用 FilePathDialog.SelectedPath 获取文件夹的路径我也知道图标的路径但我不知道如何设置该文件夹的图标

最佳答案

设置一些文件属性似乎很重要。

基于 https://github.com/dimuththarindu/FIC-Folder-Icon-Changer这是精简版。

在要设置其属性的文件夹中,创建三个文件:

  • MyIcon.ico

  • 您要显示的图标。您可以使用不同的文件名。

  • desktop.ini - 包含以下文本

    [.ShellClassInfo]

    IconResource=MyIcon.ico,0

    [ View 状态]

    模式=

    视频=

    FolderType=通用

  • .hidden- 包含以下文本

    桌面.ini

    MyIcon.ico

在我的案例中,文件类型是带 BOM 的 UTF-8。

然后你需要为所有三个文件设置属性

  • 隐藏

  • 只读

最后,您需要通知系统发生了变化

SHChangeNotify(0x08000000, 0x0000, (IntPtr)null, (IntPtr)null);

假设您创建了一个 Visual Studio 解决方案,其中包含一个名为 Resources 的文件夹,其中包含您的三个文件,下面是设置图标的代码:

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace SetFolderIcon
{
class Program
{
[DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern void SHChangeNotify(
int wEventId, int uFlags, IntPtr dwItem1, IntPtr dwItem2);

static void Main(string[] args)
{
string location = $"C:\\Users\\Balint\\Desktop";
string folderPath = Path.Combine(location, "My Folder");

string desktopIniPath = Path.Combine(folderPath, "desktop.ini");
string iconPath = Path.Combine(folderPath, "MyIcon.ico");
string hiddenPath = Path.Combine(folderPath, ".hidden");

Directory.CreateDirectory(folderPath);

File.Copy($"Resources\\desktop.ini", desktopIniPath);
File.Copy($"Resources\\Klinng.ico", iconPath);
File.Copy($"Resources\\.hidden", hiddenPath);

File.SetAttributes(desktopIniPath,
File.GetAttributes(desktopIniPath)
| FileAttributes.Hidden
| FileAttributes.ReadOnly);
File.SetAttributes(iconPath,
File.GetAttributes(iconPath)
| FileAttributes.Hidden
| FileAttributes.ReadOnly);
File.SetAttributes(hiddenPath,
File.GetAttributes(hiddenPath)
| FileAttributes.Hidden
| FileAttributes.ReadOnly);
File.SetAttributes(folderPath,
File.GetAttributes(folderPath)
| FileAttributes.ReadOnly);

SHChangeNotify(0x08000000, 0x0000, (IntPtr)null, (IntPtr)null);
}
}
}

关于C# 如何设置文件夹图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6531898/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com