Skip to content

Commit 8b6d7cf

Browse files
committed
register app for local user, does not require admin rights
1 parent b5a1e3b commit 8b6d7cf

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

PasteAsFile/Program.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public static void UnRegisterApp()
4343
{
4444
try
4545
{
46-
var key = Registry.ClassesRoot.OpenSubKey("Directory").OpenSubKey("Background").OpenSubKey("shell",true);
47-
key.DeleteSubKeyTree("Paste As File");
46+
var key = OpenDirectoryKey().OpenSubKey(@"Background\shell", true);
47+
key.DeleteSubKeyTree("Paste As File");
4848

49-
key = Registry.ClassesRoot.OpenSubKey("Directory").OpenSubKey("shell", true);
50-
key.DeleteSubKeyTree("Paste As File");
49+
key = OpenDirectoryKey().OpenSubKey("shell", true);
50+
key.DeleteSubKeyTree("Paste As File");
5151

5252
MessageBox.Show("Application has been Unregistered from your system", "Paste As File", MessageBoxButtons.OK, MessageBoxIcon.Information);
5353

@@ -63,12 +63,12 @@ public static void RegisterApp()
6363
{
6464
try
6565
{
66-
var key = Registry.ClassesRoot.OpenSubKey("Directory").OpenSubKey("Background").OpenSubKey("shell",true).CreateSubKey("Paste As File");
67-
key = key.CreateSubKey("command");
66+
var key = OpenDirectoryKey().CreateSubKey(@"Background\shell").CreateSubKey("Paste As File");
67+
key = key.CreateSubKey("command");
6868
key.SetValue("", Application.ExecutablePath + " \"%V\"");
6969

70-
key = Registry.ClassesRoot.OpenSubKey("Directory").OpenSubKey("shell",true).CreateSubKey("Paste As File");
71-
key = key.CreateSubKey("command");
70+
key = OpenDirectoryKey().CreateSubKey("shell").CreateSubKey("Paste As File");
71+
key = key.CreateSubKey("command");
7272
key.SetValue("", Application.ExecutablePath + " \"%1\"");
7373
MessageBox.Show("Application has been registered with your system", "Paste As File", MessageBoxButtons.OK, MessageBoxIcon.Information);
7474

@@ -100,5 +100,10 @@ public static void RestartApp()
100100
}
101101
Application.Exit();
102102
}
103-
}
103+
104+
static RegistryKey OpenDirectoryKey()
105+
{
106+
return Registry.CurrentUser.CreateSubKey(@"Software\Classes\Directory");
107+
}
108+
}
104109
}

PasteAsFile/frmMain.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ private void frmMain_Load(object sender, EventArgs e)
3333
txtFilename.Text = DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss");
3434
txtCurrentLocation.Text = CurrentLocation ?? @"C:\";
3535

36-
if (Registry.GetValue(@"HKEY_CLASSES_ROOT\Directory\Background\shell\Paste As File\command", "", null) == null)
37-
{
36+
if (Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\Paste As File\command", "", null) == null)
37+
{
3838
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)
3939
{
4040
Program.RegisterApp();

0 commit comments

Comments
 (0)