I use FilePathDialog.SelectedPath get folder's path I also know icon's path but i d开发者_运维百科on't know how to set that folder's icon
It seems important to set some file attributes.
Based on https://github.com/dimuththarindu/FIC-Folder-Icon-Changer here is the slimmed down version.
Within the folder, whose attribute you want to set, create three files:
MyIcon.ico
The icon you want to display. You may use a different file name.
desktop.ini - Containing the below text
[.ShellClassInfo]
IconResource=MyIcon.ico,0
[ViewState]
Mode=
Vid=
FolderType=Generic
.hidden- Containing the below text
desktop.ini
MyIcon.ico
File types in my case were UTF-8 with BOM.
Then you need to set attributes for all three files to
hidden
readonly
Finally, you need to notify the system that a change occurred
SHChangeNotify(0x08000000, 0x0000, (IntPtr)null, (IntPtr)null);
Assuming you created a Visual Studio solution with a folder named Resources holding your three files, here's the code to set the icon:
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);
}
}
}
You have to write the desktop.ini
file.
[.ShellClassInfo]
IconResource=Icon.ico,0
IconFile=Icon.ico
IconIndex=0
[ViewState]
Mode=
Vid=
FolderType=Pictures
C# code
string dir = "Folder Path";
string[] lines = { "[.ShellClassInfo]", "IconResource=Icon.ico,0", "[ViewState]", "Mode=", "Vid=", "FolderType=Pictures" };
File.WriteAllLines(dir + @"\desktop.ini", lines);
IconResource: {Icon Path},0
FolderTypes: Generic
, Documents
, Pictures
, Music
, Videos
If you need more information, check this GitHub project: https://github.com/FIC-Folder-Icon-Changer
There are basically two steps involved in assigning an icon to a folder (or possibly three steps, if you count creating the folder):
Create a desktop.ini file inside the folder for which to create the icon (the "Target Folder"). Set the Target Folder's attribute to "System".
more :
Create Icons for Folders in Windows Explorer, Using C#
this website has a very similar example
精彩评论