【问题标题】:Associate a file extension to an application within C# application将文件扩展名关联到 C# 应用程序中的应用程序
【发布时间】:2014-02-28 00:21:06
【问题描述】:

我需要将文件扩展名关联到特定的可执行应用程序并将其值写入寄存器,所以我看到了这个教程: tutorial

所以我创建了一个新的 Wpf 应用程序,我在其中添加了这个类:

public class FileAssociation
    {
        // Associate file extension with progID, description, icon and application
        public static void Associate(string extension,
               string progID, string description, string icon, string application)
        {
            Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID);
            if (progID != null && progID.Length > 0)
                using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID))
                {
                    if (description != null)
                        key.SetValue("", description);
                    if (icon != null)
                        key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon));
                    if (application != null)
                        key.CreateSubKey(@"Shell\Open\Command").SetValue("",
                                    ToShortPathName(application) + " \"%1\"");
                }
        }

        // Return true if extension already associated in registry
        public static bool IsAssociated(string extension)
        {
            return (Registry.ClassesRoot.OpenSubKey(extension, false) != null);
        }

        [DllImport("Kernel32.dll")]
        private static extern uint GetShortPathName(string lpszLongPath,
            [Out] StringBuilder lpszShortPath, uint cchBuffer);

        // Return short path format of a file name
        private static string ToShortPathName(string longName)
        {
            StringBuilder s = new StringBuilder(1000);
            uint iSize = (uint)s.Capacity;
            uint iRet = GetShortPathName(longName, s, iSize);
            return s.ToString();
        }
    }

然后,我将图标图像添加到项目的根目录中,并将这个 sn-p :

  if (!FileAssociation.IsAssociated(".akp"))
                FileAssociation.Associate(".akp", "ClassID.ProgID", "akp File", "akeo.ico", @"C:\Users\Lamloumi\Desktop\MyWork\C#\App - SuiteTool\bin\x64\Debug\App - SuiteTool.exe");

但我在这一行遇到了问题

Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID);

所以我需要知道

  1. 什么问题,即为什么这段代码不起作用?
  2. 我该如何解决?

谢谢,

【问题讨论】:

    标签: c# .net wpf file registerhotkey


    【解决方案1】:

    看来您没有足够的权限在 Windows 注册表中写入 尝试将内容添加到项目 app.manifest 文件中:

    <?xml version="1.0" encoding="utf-8"?>
    <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
            <security>
              <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
              </requestedPrivileges>
            </security>
          </trustInfo>
    </asmv1:assembly>
    

    或者如果应用程序必须在每次启动时不请求管理员权限的情况下工作,则添加自我提升的能力,如本示例所示UAC self-elevation

    【讨论】:

    • DimaS 不错,只是图标图像与文件扩展名无关
    • 带有C# FileAssociation Class关联图标和文件扩展名对我来说效果很好
    猜你喜欢
    • 2011-02-10
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多