- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Text;
- usingMicrosoft.Win32;
- namespaceFileAssociationLib
- {
- classFileAssociation
- {
- ///<summary>
- ///写注册表创建文件关联
- ///</summary>
- ///<paramname="fileFormat">文件格式(.png,.jpg...)</param>
- ///<paramname="filePath">程序执行路径</param>
- ///<returns>是否创建成功</returns>
- publicstaticboolCreateFileAssociation(stringfileFormat,stringfilePath)
- {
- boolisCreated=true;
- try
- {
- stringfileName=fileFormat.Split('.')[1]+"File";
- RegistryKeykey,keyico;
- key=Registry.ClassesRoot.CreateSubKey(fileName);
- key.SetValue("",filePath);
- //创建图标关联
- keyico=key.CreateSubKey("DefaultIcon");
- keyico.SetValue("",filePath+",0");
- //创建格式关联
- key=key.CreateSubKey("Shell");
- key=key.CreateSubKey("Open");
- key=key.CreateSubKey("Command");
- key.SetValue("","/""+filePath+"/"/"%1/"");
- key=Registry.ClassesRoot.CreateSubKey(fileFormat);
- key.SetValue("",fileName);
- }
- catch
- {
- isCreated=false;
- }
- returnisCreated;
- }
- }
- }
写注册表创建文件关联
最新推荐文章于 2022-07-06 14:27:17 发布