欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#关联自定义文件类型到应用程序并实现自动导入功能

程序员文章站 2022-06-22 16:22:15
这几天开发遇到一个需求: 将自定义文件后缀添加默认图标双击 自定义的文件后缀可以自动关联到指定的应用程序并自动打开上述第一、二两点其实是一个需求,本质是和注册表有关的操作,在注册表中写入默...

这几天开发遇到一个需求:

  • 将自定义文件后缀添加默认图标双击
  • 自定义的文件后缀可以自动关联到指定的应用程序并自动打开

上述第一、二两点其实是一个需求,本质是和注册表有关的操作,在注册表中写入默认图标、默认文件后缀、指定打开文件,一旦明白这个思路代码就非常清晰了。

代码中我们写入了两个注册表。
第一个结构如下图所示,其中defaulticon定义的是默认图标地址,command中存放的是指定识别的软件的运行exe路径。

C#关联自定义文件类型到应用程序并实现自动导入功能

第二个注册表如下图所示,将默认文件后缀和第一个注册表进行关联,这样就可以进行识别。

C#关联自定义文件类型到应用程序并实现自动导入功能

添加进去之后,我们发现双击文件可以打开软件,但是默认图标并没有刷新,经过查询,需要重启,但这样不是太low了吗,难道每个用户安装一次软件都要重启吗,这时候就需要实现自动对注册表进行刷新,直接引用即可,代码如下。

[dllimport("shell32.dll")]
public static extern void shchangenotify(uint weventid, uint uflags, intptr dwitem1, intptr dwitem2);

shchangenotify(0x8000000, 0, intptr.zero, intptr.zero);
using system;
using system.collections.generic;
using system.windows.forms;
using microsoft.win32;
namespace appc
{
    static class program
    {
        /// <summary> 
        /// 应用程序的主入口点。 
        /// </summary> 
        [stathread]
        static void main(string[] args) 
        { 
            application.enablevisualstyles(); 
            application.setcompatibletextrenderingdefault(false); 
 
            /// 是否通过关联打开的软件 
            if (args.length > 0) 
            {
                string path="";
                for(int i=0;i<args.length;i++)
                    path+=args[i]+" ";
                path=path.trimend(' ');
                console.writeline(path);
                console.readkey();
                //application.run(new mainform(path)); 
            }
            else 
            { 
                string keyname; 
                string keyvalue; 
                keyname = "c2file"; 
                keyvalue = "c2文件"; 
                registrykey isexcommand = null; 
                bool iscreateregistry = true; 
 
                try 
                { 
                    /// 检查 文件关联是否创建 
                    isexcommand = registry.classesroot.opensubkey(keyname); 
                    if (isexcommand == null) 
                    { 
                        iscreateregistry = true; 
                    } 
                    else 
                    { 
                        if (isexcommand.getvalue("create").tostring() == application.executablepath.tostring()) 
                        { 
                            iscreateregistry = false; 
                        } 
                        else 
                        { 
                            registry.classesroot.deletesubkeytree(keyname); 
                            iscreateregistry = true;                             
                        } 
                    } 
                } 
                catch (exception) 
                { 
                    iscreateregistry = true; 
                } 
 
                /// 假如 文件关联 还没有创建,或是关联位置已被改变 
                //if (iscreateregistry) 
                { 
                    try 
                    { 
                        registrykey key, keyico; 
                        key = registry.classesroot.createsubkey(keyname); 
                        key.setvalue("create", application.executablepath.tostring()); 
 
                        keyico = key.createsubkey("defaulticon"); 
                        keyico.setvalue("", application.executablepath + ",0"); 
                        
                        /*
                        string icofind = path.combine("resources", "c2", "icon");
                        string icofile = path.combine(system.windows.forms.application.startuppath, icofind, "icon.ico");
                        iconkey.setvalue(string.empty, icofile);
                         */
 
                        key.setvalue("", keyvalue); 
                        key = key.createsubkey("shell"); 
                        key = key.createsubkey("open"); 
                        key = key.createsubkey("command"); 
 
                        /// 关联的位置 
                        key.setvalue("", application.executablepath.tostring() + @" %1/");
 
                        /// 关联的文件扩展名,  
                        keyname = ".c2"; 
                        keyvalue = "c2file"; 
                        key = registry.classesroot.createsubkey(keyname); 
                        key.setvalue("", keyvalue); 
                    } 
                    catch (exception) 
                    { 
                    } 
                } 
                //application.run(new mainform("")); 
            } 
        }
    }
}

但是还有一个问题,就是打包,由于我们对注册表的写入是写在main函数里的,如果用visual studio自带的工具进行打包的话,也就是说每次用户在打开软件都会进行一次注册表读写的判断,虽然用户不感知,但是这样并不好。所以我们并不打算将注册表的写入放在main函数中,而是使用inno script studio这个工具进行美化,将注册表的写入放在里面,这样用户只会在安装的时候写入注册表,删除软件注册表也会自动清空。

#define myappexename "软件.exe"
#define appiconname "默认图标路径"

[registry]
root:hkcu;subkey: "{#myreginstallpath_sk}" ; valuetype:string; valuename:"{#myreginstallpath_vn}"; valuedata:"{app}";flags:uninsdeletekeyifempty
root: hkcr; subkey: ".c2"; flags: uninsdeletekey
root: hkcr; subkey: ".c2"; valuetype: string; valuename: ""; valuedata: "c2file"
root: hkcr; subkey: "c2file"; flags: uninsdeletekey
root: hkcr; subkey: "c2file\defaulticon"; flags: uninsdeletekey
root: hkcr; subkey: "c2file\defaulticon"; valuetype: string; valuename: ""; valuedata: "{app}\{#appiconname}"; flags: 
root: hkcr; subkey: "c2file\shell"; flags: uninsdeletekey
root: hkcr; subkey: "c2file\shell\open"; flags: uninsdeletekey
root: hkcr; subkey: "c2file\shell\open\command"; flags: uninsdeletekey
root: hkcr; subkey: "c2file\shell\open\command"; valuetype: string; valuename: ""; valuedata: "{app}\{#myappexename} ""%1"""; flags:

到此这篇关于c#关联自定义文件类型到应用程序并实现自动导入的文章就介绍到这了,更多相关c#应用程序自动导入内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!