编程实现修改Windows指定文件夹图标

思路

  • 在指定的文件夹下添加desktop.ini文件
  • cmd命令更新文件夹配置

desktop.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
; 文件夹图标
[.ShellClassInfo]
;设置文件夹的备注
InfoTip=this is temp file
;局限性资源名称,会修改文件的显示名称,但是原始的文件名不变
LocalizedResourceName=MyTempFilePRo
;设置icon的顺序
IconIndex=mainicon
;设置icon位置
IconFile=%userprofile%\Desktop\bitbug_favicon.ico

; 文件夹背景
[ExtShellFolderViews]
[{BE098140-A513-11D0-A3A4-00C04FD706EC}]
;定义图片背景
IconArea_Image=%userprofile%\Desktop\temp.jpg
;定义文字的颜色
IconArea_Text=0x000000FF

;标示文件夹所有者
[DeleteOnCopy]
Owner=Temp
;备注
PersonalizedName=My Temp file

cmd命令

  1. 隐藏配置文件

    1
    attrib +h 文件夹路径/desktop.ini
  2. 刷新文件夹,并设置为系统类型

    1
    attrib +s 文件夹路径

C#编码实现

IniFiles.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace SHSHModifyFolderIcon
{
class IniFiles
{
public string inipath;

//声明API函数

[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// 构造方法
/// </summary>
/// <param name="INIPath">文件路径</param>
public IniFiles(string INIPath)
{
inipath = INIPath;
}

public IniFiles() { }

/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="Section">项目名称(如 [TypeName] )</param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.inipath);
}
/// <summary>
/// 读出INI文件
/// </summary>
/// <param name="Section">项目名称(如 [TypeName] )</param>
/// <param name="Key"></param>
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
return temp.ToString();
}
/// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns>布尔值</returns>
public bool ExistINIFile()
{
return File.Exists(inipath);
}

}
}

ModifyFolderIcon.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System.Diagnostics;
using System.IO;


namespace SHSHModifyFolderIcon
{
public class ModifyFolderIcon
{
/// <summary>
/// 修改文件夹图标
/// </summary>
/// <param name="folPth">文件夹路径</param>
/// <param name="icoPth">图标路径</param>
/// <param name="folTip">文件夹描述信息</param>
public void Modify(string folPth, string icoPth, string folTip = null)
{
IniFiles ini = new IniFiles(folPth + @"\desktop.ini");
ini.IniWriteValue(".ShellClassInfo", "InfoTip", "系统项目文件");
ini.IniWriteValue(".ShellClassInfo", "IconIndex", "mainicon");
ini.IniWriteValue(".ShellClassInfo", "IconFile", icoPth);
if (folTip != null)
{
ini.IniWriteValue(".ShellClassInfo", "InfoTip", folTip);
}

string cmdStr1 = "attrib +h " + folPth + @"\desktop.ini";
string cmdStr2 = "attrib +s " + folPth;

Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//输出信息
p.StartInfo.RedirectStandardOutput = false;
// 输出错误
p.StartInfo.RedirectStandardError = false;
//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
//启动程序
p.Start();
//向cmd窗口发送输入信息
p.StandardInput.WriteLine(cmdStr1 + "&" + cmdStr2 + "&exit");
p.StandardInput.AutoFlush = true;
//等待程序执行完退出进程
p.WaitForExit();
p.Close();

}
/// <summary>
/// 还原文件夹图标
/// </summary>
/// <param name="folPth">文件夹路径</param>
public void Reduction(string folPth)
{
File.Delete(folPth + @"\desktop.ini");
}
}
}

插件

插件 —— 提取码:shsh

源码 ——提取码:shsh

A )