C# Denied Access Deleting Registry Value


When attempting to create a RunOnStartup Function that checks weather or not a key exists and if it does, does the user want it deleted, I encounter the problem of Access Denied. More specifically this.

System.UnauthorizedAccessException: 'Cannot write to the registry key.'

My code for this here.

private static void RunOnStartup() { string KeyName = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; string valueName = "MyApp"; if (Registry.GetValue(KeyName, valueName, null) == null) { RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); reg.SetValue("MyApp", Application.ExecutablePath.ToString()); MessageBox.Show("The Program will now start on startup", "Startup"); } else { DialogResult dialogResult = MessageBox.Show("This Program can already run on Start up. Do you want it to no longer do so?", "Start Up", MessageBoxButtons.YesNoCancel); if(dialogResult == DialogResult.Yes) { Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run").DeleteValue("MyApp"); } else if(dialogResult == DialogResult.No) { MessageBox.Show("The Program will continue to run on Startup", "Startup", MessageBoxButtons.OK); } else if(dialogResult == DialogResult.Cancel) { //Do Nothing } } }

I can create the key, Just not delete it, quite strange. Perhaps there is a permission I'm missing, I attempted to run in administrative mode but the same thing happened.


Two errors in your code:


The exception UnauthorizedAccessException - 'Cannot Write to the registry key' indicates that the you didn't open the RegistryKey in writable mode. Instead you should open it in write mode before trying to delete. Make sure you pass true as the second argument, like this:

RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\..", true); reg.DeleteValue("MyApp"); </li> <li>

Also initially your KeyName and if condition check in HKEY_LOCAL_MACHINE whereas your insertion/deletion later refer to HKEY_CURRENT_USER using Registry.CurrentUser so you should probably make them consistent.

string KeyName = @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; </li> </ul>


  • To check installed software using a program
  • Failed to connect to server. Error: 0x8007000E
  • How to save Windows theme part using GetThemeStream? (other than DWMWINDOW)
  • How to toggle “Use automatic configuration script”
  • FSC: Error FS2024: Static linking may not use assembly that targets different profile
  • Eclipse: missing requirement E4 RCP patch when installing properties editor
  • disablinging autorecover option for powepoint
  • Compress a file with GZipStream while maintaining its meta-data
  • Synchronize windows folders
  • powershell Get-Counter -ComputerName parameter on Windows 7
  • Xamarin MonoAndroid Azure mobile service InsertAsync
  • Private IP address in reserved subnet range
  • How To Customize ASP.NET Chart Databound To SqlDataSource
  • configure: error: no acceptable C compiler found in $PATH
  • Eclipse CDT error: Unable to compile
  • Spring Data JPA custom method causing PropertyReferenceException
  • req.body is undefined - nodejs
  • C# - Serializing and deserializing static member
  • Sony Xperia Z Tablet not found by adb
  • Perl system calls when running as another user using sudo
  • sending/ receiving email in Java
  • Javascript convert timezone issue
  • How to set my toolbar fixed while scrolling android
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • AT Commands to Send SMS not working in Windows 8.1
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper way to use connect-multiparty with express.js?
  • How do you join a server to an Active Directory (domain)?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • apache spark aggregate function using min value
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?