Cannot write to the registry key

Posted on 2008-11-03
Medium Priority
Last Modified: 2013-12-17
I'm getting the following error when I try to write to the Vista registry:

System.UnauthorizedAccessException was unhandled
  Message="Cannot write to the registry key."
       at Microsoft.Win32.RegistryKey.EnsureWriteable()
       at Microsoft.Win32.RegistryKey.SetValue(String name, Object value, RegistryValueKind valueKind)
       at RegistryCheck.Program.Main(String[] args) in C:\RegistryCheck\Program.cs:line 75
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

I'm runing Visual Studio as Administrator but this doesn't make a difference.  What am I missing - both for the sake of debug and as a final executable?

Question by:John500
  • 4
  • 4
LVL 10

Expert Comment

ID: 22870666
Please take a look at the last post of this thread, this should help you:

Author Comment

ID: 22870850
Thanks.  The last post goes like this:

Sorry if I wasn't clear, but you have to enable "registry object access"
auditing, that is from regedit select the key you want to audit and add the
user or group you want to monitor. Then, using "Local Security Policy"
editor, you must turn-on; Local Policies - Audit Policy - Audit Object
Access for Failure. When done, each unsuccessful access will be logged in
security log and will contain the account used to modify a key or subkey.

Can you interpret this for me?  How is this done?  The writer states the requirement but doesn't demonstrate it.
LVL 10

Expert Comment

ID: 22870930
Try and disable the UAC first following these instructions:

This article also explains what vista changed regarding the administrator account...
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.


Author Comment

ID: 22871222
The UAC is turned off but this doesn't make a difference.  As I said, I'm also running Visual Studio as administrator.

The article doesn't talk about C# code or .Net provisions for this problem......
LVL 10

Expert Comment

ID: 22871251
Please follow the instructions here:

Author Comment

ID: 22871406
I don't get it - this article talks about enabling Auditing.  If you want to log the changes that occur on a particular item then these steps would apply but not to C# code that needs access to the registry.
LVL 10

Accepted Solution

tboy6423 earned 2000 total points
ID: 22871441
This is an app your creating correct? Did you set the OpenSubKey() function to true ? (sorry about the other posts, got it confused with another answer I am working on :)

Author Comment

ID: 22871600
No, I didn't and that was the problem.  Thanks!

How about taking a look at this question too please!


Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm a big fan of Windows' offline folder caching and have used it on my laptops for over a decade.  One thing I don't like about it, however, is how difficult Microsoft has made it for the cache to be moved out of the Windows folder.  Here's how to …
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question