Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add my WPF Application to run automatically on User Login only Once. So, I am using the following code on Application Startup

public void SetStartUpRegistry()
{
     RegistryKey registryKeys = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows \\CurrentVersion\\RunOnce");
           string[] appKeyNames = registryKeys.GetSubKeyNames();

           var v = from x in appKeyNames
                   where appKeyNames.Contains("KeyName")
                   select x;

           if (v.Count() == 0)
           {
               RegistryKey regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce", true);
               string startPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs)
                  + @"\CompanyName\AppName.appref-ms";
               regkey.SetValue("KeyName", "\"" + startPath + "\"");
           }
       }


This code run perfectly when I run it in VS but when I run it from exe or I deploy it using ClickOnce it throws exception on regkey.SetValue line "attempted to perform an unauthorized operation".

I can even create new subkey in this Registry Key replacing regkey.setValue with regKey.CreateSubKey method. So, I guess it can't be Access Permission issue.

I have complete rights on registry files, I even tried it with administrator account.

What can be the Problem with this code?

I thought Registry.CurrentUser can be accessed by user without any administrative privileges.

The code runs properly if I use Run rather than RunOnce, but I want to use RunOnce.
Posted
Updated 26-Mar-14 20:39pm
v3
Comments
Bernhard Hiller 27-Mar-14 3:26am    
Are you sure it is due to insufficient rights? What about that entry already existing? Did you check that?
Varsha Ramnani 27-Mar-14 4:43am    
I have Already checked for Existing entry in the code so that is not the issue.
As for insufficient rights I don't think that is a issue as well because when i run the code in visual studio with the same profile it runs properly and when I run the exe from the project debug it throws this "unauthorized operation" error.

HKey_Current_User does not need any special access rights.

You have insufficient privileges. On Windows 7 and later, it's not enough to work with an administrative account, you also need to elevate privilege of the application through "Run As Administrator". Please try. This is how: http://www.sevenforums.com/tutorials/11841-run-administrator.html[^].

If it works, you may decide to create and embed an appropriate application manifest to request elevation automatically. Of course, it won't bypass the UAC dialog. Please see: http://msdn.microsoft.com/en-us/library/bb756929.aspx[^].

—SA
 
Share this answer
 
Comments
Varsha Ramnani 27-Mar-14 3:05am    
But I am trying to use HKey_Current_User and it works properly from Visual Studio it just doesn't work from exe.
And It also works fine when I am using "Run" from the same Registry instead of "RunOnce".
Sergey Alexandrovich Kryukov 27-Mar-14 3:10am    
Did you try what I suggested?
—SA
Varsha Ramnani 27-Mar-14 3:17am    
Yes I tried it using Run As And it still shows the same problem
 
Share this answer
 
Comments
Varsha Ramnani 27-Mar-14 23:58pm    
You are right those things are required if we access Key for Local Machine but I am using Key for Current User and I can edit the keys manually from regedit so I don't think it is the issue with UAC.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900