private void UpdateFileAssociationsButtonState() { if (CurrentOS.IsWindows) { #if WIN32 btnAssociateFiles.Visible = true; btnAssociateFiles.Enabled = true; var fileAssociation = new TangraFileAssociations(); if (fileAssociation.Registered) { btnAssociateFiles.Text = "Re-Associate files with Tangra"; } else { btnAssociateFiles.Text = "Associate files with Tangra"; if (!fileAssociation.CanRegisterWithoutElevation) { btnAssociateFiles.FlatStyle = FlatStyle.System; WindowsHelpers.SendMessage(btnAssociateFiles.Handle, WindowsHelpers.BCM_SETSHIELD, 0, (IntPtr)1); } } if (fileAssociation.CanRegisterWithoutElevation) { btnAssociateFiles.Tag = fileAssociation; } #endif } else { btnAssociateFiles.Visible = false; } }
private void btnAssociateFiles_Click(object sender, EventArgs e) { #if WIN32 TangraFileAssociations fileAssocHelper = btnAssociateFiles.Tag as TangraFileAssociations; if (fileAssocHelper != null) { fileAssocHelper.Associate(true); UpdateFileAssociationsButtonState(); } else { // Launch itself as administrator passing the right parameter the do the job var proc = new ProcessStartInfo(); proc.UseShellExecute = true; proc.WorkingDirectory = Environment.CurrentDirectory; proc.FileName = Application.ExecutablePath; proc.Arguments = TangraFileAssociations.COMMAND_LINE_ASSOCIATE; proc.Verb = "runas"; btnAssociateFiles.Enabled = false; try { Process newProcess = new Process() { StartInfo = proc }; newProcess.Start(); newProcess.WaitForExit(); } catch { // The user refused to allow privileges elevation. // Do nothing and return directly ... return; } finally { UpdateFileAssociationsButtonState(); } } #endif }
static bool ExecuteNonUICommandLineActions() { string[] args = Environment.GetCommandLineArgs(); string action = args.Length > 1 ? args[1].Trim('"') : null; if (CurrentOS.IsWindows) { #if WIN32 bool isRegisterAssociationsCommand = TangraFileAssociations.COMMAND_LINE_ASSOCIATE.Equals(action, StringComparison.InvariantCultureIgnoreCase); try { var fileAssociation = new TangraFileAssociations(); if (!fileAssociation.Registered || isRegisterAssociationsCommand) { fileAssociation.Associate(false); } } catch (Exception ex) { if (isRegisterAssociationsCommand) { Trace.WriteLine(ex.GetFullStackTrace()); } } return(isRegisterAssociationsCommand); #else return(false); #endif } else { return(false); } }