要解决WinForm应用程序在安装后出现不需要的通用图标的问题,可以使用以下代码示例:
using Microsoft.Win32;
namespace MyWinFormApp
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// 设置指定文件类型的图标
SetFileTypeIcon(".txt", "myIcon.ico");
Application.Run(new MainForm());
}
static void SetFileTypeIcon(string extension, string iconFileName)
{
// 获取文件类型关联的注册表项
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension, true))
{
// 如果不存在,则创建新的注册表项
if (key == null)
{
using (RegistryKey extKey = Registry.ClassesRoot.CreateSubKey(extension))
{
extKey.SetValue("", "MyAppFileType");
}
}
// 创建"DefaultIcon"子键并设置图标路径
using (RegistryKey iconKey = key.CreateSubKey("DefaultIcon"))
{
iconKey.SetValue("", Application.ExecutablePath + ",0");
}
}
}
}
}
在上述代码示例中,我们通过调用SetFileTypeIcon
方法来设置文件类型的图标。该方法接受文件扩展名和ICO图标文件名作为参数,并使用注册表项来设置图标。
注意:确保将ICO图标文件添加到项目中,并将其生成操作设置为“嵌入的资源”。此外,还可以更改ICO图标文件的路径和名称以适应您的应用程序。