首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复:应用程序图标覆盖文件图标

如何修复:应用程序图标覆盖文件图标
EN

Stack Overflow用户
提问于 2019-06-17 11:55:30
回答 1查看 130关注 0票数 0

我编写了一个运行在Windows 10上的WPF应用程序,我有两个图标:一个用于应用程序,一个用于关联的文件类型(.rwa)。在安装此应用程序时,我使用寄存器设置文件关联,将文件图标指向DefaultIcon。这一切都有效,我的应用程序的快捷方式显示了App。我可以双击.rwa文件,问我要使用哪个应用程序,然后选择我的应用程序。但是,一旦我这样做了,文件图标就会更改为应用程序图标!

编辑:微软网站https://learn.microsoft.com/en-us/windows/desktop/shell/customizing-file-types-bumper上有大量关于文件类型的信息,但他们提到它不适用于Windows 10,至少在某个版本之后是这样。因此,我不确定我所作的更改是否正确。我就是这样做的:

代码语言:javascript
复制
 /// <summary>Makes the association.</summary>
    /// <param name="extension">The extension to associate.</param>
    /// <param name="progID">The program identifier.</param>
    /// <param name="description">The description.</param>
    /// <param name="icon">The full path to the icon.</param>
    /// <param name="application">The name of the application.</param>
    /// <param name="exe">The full path to the executable.</param>
    public static void MakeAssocisation(string extension,
           string progID, string description, string icon, string application, string exe)
    {
        using (var User_Classes = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Classes\\", true))
        using (var CurrentVersion = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\", true))
        using (var User_Explorer = CurrentVersion.CreateSubKey("Explorer\\FileExts\\." + extension))
        {
            string applicationPath = application;
            // Register the application
            using (var UserClassesApplications = CurrentVersion.CreateSubKey("App Paths"))
            {
                UserClassesApplications.CreateSubKey(exe).SetValue("", application);
            }
            // Create ProgID
            if (!string.IsNullOrEmpty(progID))
            {
                using (var progId_key = User_Classes.CreateSubKey(progID))
                {
                    progId_key.SetValue("", description);
                    progId_key.CreateSubKey("DefaultIcon").SetValue("", "\"" + icon + "\"");
                }
            }
            // Now the extension
            using (var User_Ext = User_Classes.CreateSubKey("." + extension))
            {
                User_Ext.SetValue("", progID);
                User_Ext.CreateSubKey("DefaultIcon").SetValue("", "\"" + icon + "\"");
            }
            User_Explorer.CreateSubKey("OpenWithProgids").SetValue(progID, "0");
            using (RegistryKey User_Choice = User_Explorer.OpenSubKey("UserChoice"))
            {
                if (User_Choice != null) User_Explorer.DeleteSubKey("UserChoice");
            }
            SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
        }
    }

当我更改注册表时,我发现.exe正确地显示了App,关联文件(.rwa)显示了正确的图标。如果我启动应用程序(例如,点击捷径),那么一切都会正常工作。但是,当我双击一个.rwa文件时,会被询问要打开哪个应用程序(这是Windows 10中的不同之处;它不使用注册表中定义的关联)。我选择了我的应用程序,它正确地启动它,但它也改变了我的.rwa文件的图标!

EN

回答 1

Stack Overflow用户

发布于 2019-06-20 09:29:45

Panagiotis是对的:在我的登记册中,我漏掉了一些信息。经过一些实验,通过查看注册表中的其他应用程序,我得出了以下工作代码:

代码语言:javascript
复制
 /// <summary>Makes the association.</summary>
    /// <param name="extension">The extension to associate.</param>
    /// <param name="progID">The program identifier.</param>
    /// <param name="description">The description.</param>
    /// <param name="icon">The full path to the icon.</param>
    /// <param name="application">The full path to the executable.</param>
    /// <param name="exe">The executable.</param>
    public static void MakeAssociation(string extension,
           string progID, string description, string icon, string application, string exe)
    {
        using (var User_Classes = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Classes\\", true))
        using (var CurrentVersion = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\", true))
        using (var User_Explorer = CurrentVersion.CreateSubKey("Explorer\\FileExts\\." + extension))
        {
            // Register the application
            using (var UserClassesApplications = CurrentVersion.CreateSubKey("App Paths"))
            {
                UserClassesApplications.CreateSubKey(exe).SetValue("", application);
            }
            // Create ProgID
            if (!string.IsNullOrEmpty(progID))
            {
                using (var progId_key = User_Classes.CreateSubKey(progID))
                {
                    progId_key.SetValue("", description);
                    using (var command = progId_key.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command"))
                    {
                        command.SetValue("", "\"" + application + "\" \"%1\"");
                    }
                    progId_key.CreateSubKey("DefaultIcon").SetValue("", "\"" + icon + "\"");
                }
            }
            // Now the extension
            using (var User_Ext = User_Classes.CreateSubKey("." + extension))
            {
                User_Ext.SetValue("", progID);
                User_Ext.CreateSubKey("DefaultIcon").SetValue("", "\"" + icon + "\"");
            }
            User_Explorer.CreateSubKey("OpenWithProgids").SetValue(progID, "0");
            using (RegistryKey User_Choice = User_Explorer.OpenSubKey("UserChoice"))
            {
                if (User_Choice != null) User_Explorer.DeleteSubKey("UserChoice");
                User_Explorer.CreateSubKey("UserChoice").SetValue("ProgId", progID);
            }
            SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
        }
    }

我还发现,路径参数不能包括%LocalAppData%这样的内容。这必须是一条扩展的道路。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56630807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档