C#实现文件与应用程序关联的方法 使用的是修改注册表方法 在网上找了几个例程运行 报错访问注册表被拒绝
static void RegFileExt()
{
try
{
string boardExeFullName = Application.ExecutablePath;
if (File.Exists(boardExeFullName))
{
string MyExtName = ".pjt";
string MyType = "dbb_auto_file";
string MyContent = "application/pjt";
string command = "\"" + boardExeFullName + "\"" + " \"%1\"";
RegistryKey key = Registry.ClassesRoot.OpenSubKey(MyType);
if (key == null)
{
RegistryKey MyReg = Registry.ClassesRoot.CreateSubKey(MyExtName);
MyReg.SetValue("", MyType);
MyReg.SetValue("Content Type", MyContent);
MyReg = Registry.ClassesRoot.CreateSubKey(MyType);
MyReg.SetValue("", MyType);
MyReg = MyReg.CreateSubKey("Shell\\Open\\Command");
MyReg.SetValue("", command);
MyReg.Close();
}
else
{
var myReg = key.OpenSubKey("Shell\\Open\\Command", true);
if (myReg != null && (myReg.GetValue("") == null || myReg.GetValue("").ToString() != command))
{
myReg.SetValue("", command);//解决因目录变化导致 注册表失效的问题
myReg.Close();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
程序导出文件关联程序 打开文件可以打开程序并读取内容
用管理员权限打开程序
管理员身份运行