Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Registry permissions denied

Status
Not open for further replies.

dg043

Programmer
Apr 22, 2002
52
0
0
GB
I am experiencing the same problem as described in thread796-1042121. I can run the program (a windows forms application which simply changes a registry value) through the Visual Studio IDE but as soon as I try and just execute the program (by running the .exe file through Windows explorer), it gives the error. Just fo reference, the code I am using is
Code:
Imports Microsoft.win32

Module modStart

    Public Sub Main()

        Dim wallpaperKey As RegistryKey

        Try
            wallpaperKey = Registry.CurrentUser.OpenSubKey("Software", True).OpenSubKey("Microsoft", True).OpenSubKey("Windows", True).OpenSubKey("CurrentVersion", True).OpenSubKey("Policies", True).OpenSubKey("System", True)
            wallpaperKey.SetValue("wallpaper", "c:\windows\bground.bmp")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
            End
        End Try

    End Sub

End Module

Dan Griffiths
Software Analyst
National Grid plc
 
Hi, can't you use:

My.Computer.Registry.CurrentUser.OpenSubKey("HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\POLICIES\SYSTEM").SetValue("wallpaper", "c:\windows\bground.bmp")

--

woogoo
 
Thanks for your suggestion but I've just tried that and the short answer is no. Firstly, there is no such object as 'My' in the application therefore the call
Code:
My.Computer.......
results in an error message of 'Object not set to an instance of an object'. Secondly, to my knowledge the Registry.CurrentUser call refers to the HKEY_CURRENT_USER key and so I would guess that this doesn't require it to be specified in the argument. Maybe the confusion is caused by the fact that I am using Visual Studio .NET 2003 and that is different to the version you are using. Anyway, if I do use the version of the code that I think you mean ie.
Code:
Reistry.CurrentUser.OpenSubKey("Sotware\Microsoft\Windows\CurrentVersion\Policies\System").SetValue("wallpaper", "c:\windows\bground.bmp")
I still receive an error that says 'object reference not set to an instance of an object'

Dan Griffiths
Software Analyst
National Grid plc
 
Well does that not imply that there is no key with that name. I have to admit when I tested I got the same result so I opened the registry using regedit and followed the path and sure enough no entry in mine.



woogoo
 
If I open regedit I can see that key and I can change it. As I said before, my original code runs fine through the VB IDE but gives an error message when I run the executable through Windows. My initial thought was that it might be something to do with permissions (eg. the IDE runs the code as one user and Windows runs the executable as another) but I've looked at the permissions on the key itself and that seems fine (I think)

Dan Griffiths
Software Analyst
National Grid plc
 


It looks like its something with your PC and not the code. I copied/pasted it into a vb2003 test app and it worked fine from the IDE and from Windows Explorer.
Then I changed the OpenSubKey a little, added a file picker and a messagebox showing the current user. Still worked correctly.

Code:
	 Public Sub ChangeWallpaper()
		  Dim wallpaperKey As RegistryKey
		  Dim pick As New OpenFileDialog

		  pick.ShowDialog()

		  Try
				MessageBox.Show("Logged in as " & Environment.UserName)
				wallpaperKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Policies\System", True)
				wallpaperKey.SetValue("wallpaper", pick.FileName)
		  Catch ex As Exception
				MsgBox(ex.Message, MsgBoxStyle.Critical)
				End
		  End Try
	 End Sub

Hope that helps.

 
Thanks for the help. If I use your code, pmegan, it works fine through both the IDE and Windows. If you notice from my previous post, there was a typo in the path of the key.

Dan Griffiths
Software Analyst
National Grid plc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top