我需要将文件扩展名与特定的可执行应用程序相关联,并将其值写入寄存器,因此我看到了本教程: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();
}
}
然后,我将图标图像添加到项目的根目录,然后放入以下代码段:
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);
所以我需要知道
谢谢,
似乎您没有足够的权限来编写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自动提升
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句