From 8b6d7cf4d0e61014ea5485e0eb0306e5d3203156 Mon Sep 17 00:00:00 2001 From: Sergey Galich Date: Thu, 13 Jul 2017 12:50:52 +0300 Subject: [PATCH] register app for local user, does not require admin rights --- PasteAsFile/Program.cs | 23 ++++++++++++++--------- PasteAsFile/frmMain.cs | 4 ++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/PasteAsFile/Program.cs b/PasteAsFile/Program.cs index 233e04c..fd366fc 100644 --- a/PasteAsFile/Program.cs +++ b/PasteAsFile/Program.cs @@ -43,11 +43,11 @@ public static void UnRegisterApp() { try { - var key = Registry.ClassesRoot.OpenSubKey("Directory").OpenSubKey("Background").OpenSubKey("shell",true); - key.DeleteSubKeyTree("Paste As File"); + var key = OpenDirectoryKey().OpenSubKey(@"Background\shell", true); + key.DeleteSubKeyTree("Paste As File"); - key = Registry.ClassesRoot.OpenSubKey("Directory").OpenSubKey("shell", true); - key.DeleteSubKeyTree("Paste As File"); + key = OpenDirectoryKey().OpenSubKey("shell", true); + key.DeleteSubKeyTree("Paste As File"); MessageBox.Show("Application has been Unregistered from your system", "Paste As File", MessageBoxButtons.OK, MessageBoxIcon.Information); @@ -63,12 +63,12 @@ public static void RegisterApp() { try { - var key = Registry.ClassesRoot.OpenSubKey("Directory").OpenSubKey("Background").OpenSubKey("shell",true).CreateSubKey("Paste As File"); - key = key.CreateSubKey("command"); + var key = OpenDirectoryKey().CreateSubKey(@"Background\shell").CreateSubKey("Paste As File"); + key = key.CreateSubKey("command"); key.SetValue("", Application.ExecutablePath + " \"%V\""); - key = Registry.ClassesRoot.OpenSubKey("Directory").OpenSubKey("shell",true).CreateSubKey("Paste As File"); - key = key.CreateSubKey("command"); + key = OpenDirectoryKey().CreateSubKey("shell").CreateSubKey("Paste As File"); + key = key.CreateSubKey("command"); key.SetValue("", Application.ExecutablePath + " \"%1\""); MessageBox.Show("Application has been registered with your system", "Paste As File", MessageBoxButtons.OK, MessageBoxIcon.Information); @@ -100,5 +100,10 @@ public static void RestartApp() } Application.Exit(); } - } + + static RegistryKey OpenDirectoryKey() + { + return Registry.CurrentUser.CreateSubKey(@"Software\Classes\Directory"); + } + } } diff --git a/PasteAsFile/frmMain.cs b/PasteAsFile/frmMain.cs index 25e5ee8..13e26e9 100644 --- a/PasteAsFile/frmMain.cs +++ b/PasteAsFile/frmMain.cs @@ -33,8 +33,8 @@ private void frmMain_Load(object sender, EventArgs e) txtFilename.Text = DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss"); txtCurrentLocation.Text = CurrentLocation ?? @"C:\"; - if (Registry.GetValue(@"HKEY_CLASSES_ROOT\Directory\Background\shell\Paste As File\command", "", null) == null) - { + if (Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\Paste As File\command", "", null) == null) + { if (MessageBox.Show("Seems that you are running this application for the first time,\nDo you want to Register it with your system Context Menu ?", "Paste As File", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Program.RegisterApp();