private void frmUpgradeBuilder_Load(object sender, EventArgs e) { #region 自动注册文件类型 if (!FileTypeRegister.FileTypeRegistered(".upb")) { FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(".upb"); fileTypeRegInfo.Description = "IRAP 自动更新工程文件"; fileTypeRegInfo.ExePath = Application.ExecutablePath.Replace('/', '\\'); fileTypeRegInfo.ExtendName = ".upb"; fileTypeRegInfo.IcoPath = Application.ExecutablePath.Replace('/', '\\'); FileTypeRegister.RegisterFileType(fileTypeRegInfo); } #endregion WindowState = FormWindowState.Maximized; #region 检查当前应用是否以管理员权限运行 WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); bool isRunasAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator); #endregion btnRegisterApp.Enabled = isRunasAdmin; if (!btnRegisterApp.Enabled) { btnRegisterApp.SuperTip = new SuperToolTip(); btnRegisterApp.SuperTip.Items.Add("当前功能需要程序具有管理员权限才能使用!"); } }
/// <summary> /// 更新指定文件类型关联信息 /// </summary> public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo) { if (!FileTypeRegistered(regInfo.ExtendName)) { return(false); } try { string extendName = regInfo.ExtendName; string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType"; RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName, true); relationKey.SetValue("", regInfo.Description); RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon", true); iconKey.SetValue("", regInfo.IconPath); RegistryKey shellKey = relationKey.OpenSubKey("Shell"); RegistryKey openKey = shellKey.OpenSubKey("Open"); RegistryKey commandKey = openKey.OpenSubKey("Command", true); commandKey.SetValue("", regInfo.ExePath + " %1"); relationKey.Close(); SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero); return(true); } catch (System.Exception) { UnityEngine.Debug.LogError("更新注册表操作失败!"); DialogResult dialogResult = MessageBox.Show("更新失败,重启并尝试以管理员身份?", "友情提示-" + UnityEngine.Application.productName, MessageBoxButtons.OKCancel); if (dialogResult == DialogResult.OK) { ReStartAs(); } return(false); } }
private void btnRegisterApp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(".upb"); fileTypeRegInfo.Description = "IRAP 自动更新工程文件"; fileTypeRegInfo.ExePath = Application.ExecutablePath.Replace('/', '\\'); fileTypeRegInfo.ExtendName = ".upb"; fileTypeRegInfo.IcoPath = Application.ExecutablePath.Replace('/', '\\'); FileTypeRegister.RegisterFileType(fileTypeRegInfo); }
private void 设置文件关联ToolStripMenuItem_Click(object sender, EventArgs e) { FileTypeRegInfo pbox = new FileTypeRegInfo(); pbox.ExtendName = ".pbox"; pbox.Description = "私密箱"; pbox.IcoPath = AppDomain.CurrentDomain.BaseDirectory + "PrivateBox.ico"; pbox.ExePath = AppDomain.CurrentDomain.BaseDirectory + "PrivateBox.exe"; FileTypeRegister.RegisterFileType(pbox); SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero); MessageBoxEx.Show("设置完毕! ", "设置文件关联"); }
public void RegisterFileType() { var info = new FileTypeRegInfo() { ExtendName = ".WXF", Description = "WSX Cut Document(.WXF)", IcoPath = Application.StartupPath + @"\Assets\file2.ico", ExePath = Application.StartupPath + @"\WSXCut.exe" }; FileAssociator.RegisterFileType(info); }
/// <summary> /// 绑定扩展名 /// </summary> private void BindingExtend() { if (!FileTypeRegister.FileTypeRegistered(SystemCommon.Extension)) { FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(SystemCommon.Extension); fileTypeRegInfo.Description = SystemCommon.SystemName + "缓存文件"; fileTypeRegInfo.ExePath = SystemCommon.ApplicationName; fileTypeRegInfo.ExtendName = SystemCommon.Extension; fileTypeRegInfo.IconPath = SystemCommon.ApplicationName; // 注册 //FileTypeRegister fileTypeRegister = new FileTypeRegister(); FileTypeRegister.RegisterFileType(fileTypeRegInfo); } }
/// <summary> /// 使文件类型与对应的图标及应用程序关联起来 /// </summary> public static void RegisterFileType(FileTypeRegInfo regInfo) { if (FileTypeRegistered(regInfo.ExtendName)) { return; } try { UnityEngine.Debug.Log("注册表添加文件系统中!"); //HKEY_CLASSES_ROOT/.osf RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName); string relationName = regInfo.ExtendName.Substring(1, regInfo.ExtendName.Length - 1).ToUpper() + "_FileType"; fileTypeKey.SetValue("", relationName); fileTypeKey.Close(); //HKEY_CLASSES_ROOT/OSF_FileType RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName); relationKey.SetValue("", regInfo.Description); //HKEY_CLASSES_ROOT/OSF_FileType/Shell/DefaultIcon RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon"); iconKey.SetValue("", regInfo.IconPath); //HKEY_CLASSES_ROOT/OSF_FileType/Shell RegistryKey shellKey = relationKey.CreateSubKey("Shell"); //HKEY_CLASSES_ROOT/OSF_FileType/Shell/Open RegistryKey openKey = shellKey.CreateSubKey("Open"); //HKEY_CLASSES_ROOT/OSF_FileType/Shell/Open/Command RegistryKey commandKey = openKey.CreateSubKey("Command"); commandKey.SetValue("", regInfo.ExePath + " %1"); // " %1"表示将被双击的文件的路径传给目标应用程序 relationKey.Close(); SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero); } catch (Exception) { UnityEngine.Debug.Log("注册表添加文件系统失败!"); DialogResult dialogResult = MessageBox.Show("添加失败,重启并尝试以管理员身份运行?", "友情提示-" + UnityEngine.Application.productName, MessageBoxButtons.OKCancel); if (dialogResult == DialogResult.OK) { ReStartAs(); } throw; } }
private void Init_RegisterFileType() { if (!System.IO.Directory.Exists(AppSetting.ApplicationData_SecuruStikPath)) { Directory.CreateDirectory(AppSetting.ApplicationData_SecuruStikPath); } File.Copy(AppSetting.App_Downloader, AppSetting.AppDataFolder_DownloaderFullPath, true); File.Copy(AppSetting.App_IconPath, AppSetting.AppDatafolder_Icon, true); FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(); fileTypeRegInfo.Description = "SecuruStik share info file"; fileTypeRegInfo.ExePath = AppSetting.AppDataFolder_DownloaderFullPath; fileTypeRegInfo.ExtendName = AppSetting.ShareInfoFileExtension; fileTypeRegInfo.IcoPath = AppSetting.AppDatafolder_Icon; FileTypeRegister.RegisterFileType(fileTypeRegInfo); }
public void CreatRegData() { if (!FileTypeRegister.FileTypeRegistered(filetype)) { Debug.LogFormat("文件类型 {0} 注册中!",filetype); FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(filetype) { Description = "测试自定义文件系统", ExePath = System.Environment.GetCommandLineArgs()[0], ExtendName = filetype, IconPath = Path.Combine(UnityEngine.Application.streamingAssetsPath, string.Format("{0}.ico", iconName)) }; // 注册 FileTypeRegister fileTypeRegister = new FileTypeRegister(); FileTypeRegister.RegisterFileType(fileTypeRegInfo); } else { Debug.LogFormat("文件类型 {0} 已经存在中!", filetype); } }
/// <summary> /// 获取指定文件类型关联信息 /// </summary> public static FileTypeRegInfo GetFileTypeRegInfo(string extendName) { if (!FileTypeRegistered(extendName)) { return(null); } FileTypeRegInfo regInfo = new FileTypeRegInfo(extendName); string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType"; RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName); regInfo.Description = relationKey.GetValue("").ToString(); RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon"); regInfo.IconPath = iconKey.GetValue("").ToString(); RegistryKey shellKey = relationKey.OpenSubKey("Shell"); RegistryKey openKey = shellKey.OpenSubKey("Open"); RegistryKey commandKey = openKey.OpenSubKey("Command"); string temp = commandKey.GetValue("").ToString(); regInfo.ExePath = temp.Substring(0, temp.Length - 3); return(regInfo); }
private void Start() { if (FileTypeRegister.FileTypeRegistered(filetype)) { Debug.Log("文件类型已经存在~-"+filetype); FileTypeRegInfo regInfo =FileTypeRegister.GetFileTypeRegInfo(filetype); if (!File.Exists(regInfo.ExePath)||regInfo.ExePath!=executePath) { DialogResult dialogResult= MessageBox.Show(string.Format("文件系统{0}丢失打开方式,是否修复?", filetype),"警告:文件系统异常",MessageBoxButtons.OKCancel); if (dialogResult==DialogResult.OK) { regInfo.ExePath = executePath; regInfo.IconPath = Path.Combine(UnityEngine.Application.streamingAssetsPath, string.Format("{0}.ico", iconName)); FileTypeRegister.UpdateFileTypeRegInfo(regInfo); } } } else { Debug.LogWarning("文件类型不存在~-" + filetype); MessageBox.Show("文件系统尚未创建!"); } }
/// <summary> /// 安装时注册CAP文件 /// </summary> /// <param name="savedState"></param> public override void Install(IDictionary stateSaver) { base.Install(stateSaver); string currentInstallPath = this.Context.Parameters["targetdir"]; currentInstallPath = currentInstallPath.Replace("\\/", "\\"); #region 注册CAP FileTypeRegInfo reginfo = new FileTypeRegInfo(".cap"); reginfo.ExePath = currentInstallPath; //Application.ExecutablePath; reginfo.Description = "CAPP程序"; reginfo.IcoPath = currentInstallPath + @"Resources\cap.ico"; try { FileTypeRegister.RegisterFileType(reginfo); } catch { } #endregion #region 修改CAD默认菜单 /// 判断是否已经注册AutoCAD if (!FileTypeRegister.IsRegeditExit()) { try { FileTypeRegister.RegisterAutoCADPlugIn(currentInstallPath); } catch { } } /// 增加工艺简图菜单到AutoCAD 中 FileTypeRegister.UpdateAutoCADMenu(currentInstallPath); #endregion #region 注册Solidworks 插件 try { Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine; Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser; string keyname = "SOFTWARE\\SolidWorks\\Addins\\{2eff6d31-932a-4191-ad00-1d705e27a64f}"; Microsoft.Win32.RegistryKey addinkey = hklm.CreateSubKey(keyname); addinkey.SetValue(null, 0); addinkey.SetValue("Description", "Solidworks AddIn Description"); addinkey.SetValue("Title", "Routing"); keyname = "Software\\SolidWorks\\AddInsStartup\\{2eff6d31-932a-4191-ad00-1d705e27a64f}"; addinkey = hkcu.CreateSubKey(keyname); addinkey.SetValue(null, true, Microsoft.Win32.RegistryValueKind.DWord); } catch (System.NullReferenceException nl) { Console.WriteLine("There was a problem registering this dll: SWattr is null. \n\"" + nl.Message + "\""); } #endregion #region 解决 Win7不能操作注册表问题 /** * 当前用户是管理员的时候,直接启动应用程序 * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 */ //获得当前登录的Windows用户标示 //System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); //System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); ////判断当前登录用户是否为管理员 //if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) //{ // //如果是管理员,则直接运行 // if (string.IsNullOrEmpty(MainFrm.FilePath)) // { // var login = new LoginFrm(); // if (login.ShowDialog() == DialogResult.OK) // { // Application.Run(new MainFrm()); // } // } // else // { // Application.Run(new MainFrm()); // } //} //else //{ // //创建启动对象 // System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); // //设置运行文件 // startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; // //设置启动参数 // startInfo.Arguments = String.Join(" ", args); // //设置启动动作,确保以管理员身份运行 // startInfo.Verb = "runas"; // //如果不是管理员,则启动UAC // System.Diagnostics.Process.Start(startInfo); // //退出 // System.Windows.Forms.Application.Exit(); //} #endregion }