我编写了一个在Windows 10上运行的WPF应用程序。我有两个图标:一个用于应用程序,一个用于关联的文件类型(.rwa)。安装此应用程序时,我使用寄存器设置文件关联,将“文件图标”指向DefaultIcon。所有这些都有效,我的应用程序的快捷方式显示“应用程序图标”。 我可以双击.rwa文件,Windows会问我要使用哪个应用程序,然后选择我的应用程序。但是,一旦完成此操作,文件图标就会更改为应用程序图标!
编辑:Microsoft网站https://docs.microsoft.com/en-us/windows/desktop/shell/customizing-file-types-bumper上有大量有关文件类型的信息,但他们提到它不适用于Windows 10 ,至少在某些版本之后。因此,我不确定在注册表中所做的更改是否正确。 这就是我要做的:
/// <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正确显示了应用程序图标,而相关文件(.rwa)显示了正确的图标。如果我启动该应用程序(例如,单击快捷方式),则一切正常。但是,当我双击.rwa文件时,系统会询问我要打开哪个应用程序(这与Windows 10不同;它不使用注册表中定义的关联)。我选择了我的应用程序,它可以正确启动,但是它也会更改我的.rwa文件的图标!
答案 0 :(得分:0)
Panagiotis是正确的:在设置寄存器的过程中,我错过了一些信息。经过一些试验,并通过查看注册表中的其他应用程序,我想到了以下工作代码:
/// <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%之类的内容。它必须是扩展路径。