AxRoss

Hi !!

I've to write on registry in HKEY_LOCAL_MACHINE\SOFTWARE\MySoft how can I do I try with this source:

Code Snippet

using System.Security.Permissions;

using Microsoft.Win32;

[assembly: SecurityPermission(SecurityAction.RequestMinimum)]

[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, ViewAndModify = HKEY_LOCAL_MACHINE")]

.....

public static void SetRegistryKey(string SubKey, string Name, string SubKeyValue)

{

RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\MySoft\\" + SubKey);

if (key != null)

{

key.SetValue(Name.ToString(), SubKeyValue.ToString());

}

else

{

key.CreateSubKey(SubKey.ToString());

key.SetValue(Name.ToString(), SubKeyValue.ToString());

}

key.Close();

}

But when I try to execute the write command (setValue) an error is shown: "You don't have the permission to write...."

I don't know how is possible. If open / edit /write with REGEDIT, there's no problem, everything work !!!

Since, is possible that the USER (not me...), has not the permission to write, but I HAVE TO WRITE IN THAT LOCATION, is possible to set the permission via software, only for writing this key I don't want to change the grants for that user, I need only to access (and modify) the information stored in the registry in HKEY_LOCAL_MACHINE\SOFTWARE\MySoft

How can I do

THANKS A LOT !!

Alex



Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

H. Tony

if you need to access registry when user runs the program doesnt have the right, you need to consider impersonation in this thread

some helper class info from that thread

Windows Impersonation using C#

Impersonation class, C# Source





Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

eradicator

key.SetValue(Name.ToString(), SubKeyValue.ToString());

By the way, invoking ToString() on variables which are declared as string type, is pretty pointless.




Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

AxRoss

Thanks !!

I didn't know what are the "impersonation". The links are very interesting.

I'm going to try the Impersonation for Windows Form that you suggest.

One think I don't understant... in my program I don't have to authenticate user. Why I have to "impersonate" myself I have all administration grant for my user. This mean that I can do everything on my localy stored registry (for example executing REGEDIT), why launching the application it didn't work The grant are non the same

Thanks

Alex





Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

AxRoss

eradicator wrote:

key.SetValue(Name.ToString(), SubKeyValue.ToString());

By the way, invoking ToString() on variables which are declared as string type, is pretty pointless.

Thanks

it's true !! Is really pointless.

Thank

Alex





Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

H. Tony

since the run who runs the program doesnt have the permission and you HAVE TO let the program access the registry, you need to IMPERSONATE which ever account that has the permission to let the program have the permissoin to run as that user to access the registry.

do you mean that you have the right already, but you still cant access the registry

I am confused since you mentioned you can access it but user (not you) cannot.

AxRoss wrote:

Thanks !!

I didn't know what are the "impersonation". The links are very interesting.

I'm going to try the Impersonation for Windows Form that you suggest.

One think I don't understant... in my program I don't have to authenticate user. Why I have to "impersonate" myself I have all administration grant for my user. This mean that I can do everything on my localy stored registry (for example executing REGEDIT), why launching the application it didn't work The grant are non the same

Thanks

Alex






Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

AxRoss

H. Tony wrote:
since the run who runs the program doesnt have the permission and you HAVE TO let the program access the registry, you need to IMPERSONATE which ever account that has the permission to let the program have the permissoin to run as that user to access the registry.

do you mean that you have the right already, but you still cant access the registry

I am confused since you mentioned you can access it but user (not you) cannot.

AxRoss wrote:

Thanks !!

I didn't know what are the "impersonation". The links are very interesting.

I'm going to try the Impersonation for Windows Form that you suggest.

One think I don't understant... in my program I don't have to authenticate user. Why I have to "impersonate" myself I have all administration grant for my user. This mean that I can do everything on my localy stored registry (for example executing REGEDIT), why launching the application it didn't work The grant are non the same

Thanks

Alex

H. Tony wrote:

do you mean that you have the right already, but you still cant access the registry

re: Yes, my windows account have all the privilege!! but I can't still modify the registry (with the application, only) .. I can read the HKEY_LOCAL_MACHINE but I can't edit. If I try to write (with the same source...) on HKEY_CURRENT_USER, everything work fine. Also using REGEDIT, I can edit the HKEY_LOCAL_MACHINE\SOFTWARE\ ......

Excuse me!! I'm going to made a little confusion..

Thanks

Alex





Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

eradicator

Are you sure this application is running under your account It seems like it isn't. Is this a web application / web service (in which case it likely wouldn't be running under your account unless you set it up in IIS to run as you)






Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

AxRoss

No it isn't a web application, is a Windows application that work localy except for reading information from SqlServer 2005 (with a trusted connection).

I'm quite sure that this application work with my account. I don't make any authentication.





Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

eradicator

What does SqlServer 2005 have to do with this problem of writing to the registry You aren't running a stored procedure on SqlServer and IT is the one trying to write to the registry, right






Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

AxRoss

right.

Sql2005 have nothing to do with this problem. I don't use SQL2005 to write on registry (IS IT POSSIBLE ).

SqlServer 2005 connection work fine. I don't have any problem with this. The problem is only to write on Windows Registry. I specified only that my application doesn't use any other connection, except one trusted connection with SQL 2005. It's an application that work localy.

I have to read/write on registry at that location 'cause I've to take a path (used by one other application) of an xml config file. This form have to write the location (Path and Name) of this file on registry, and the other application will read (and use) this path.





Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

H. Tony


pretty clueless, have you tried removing the following from your code

[assembly: SecurityPermission(SecurityAction.RequestMinimum)]

[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, ViewAndModify = HKEY_LOCAL_MACHINE")]







Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

AxRoss

Hi H.Tony !

Yes I have. I've tried, but nothing changed.





Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

AxRoss

Thanks to all !!

I find the solution!!!!! .

The solution was very, very easy :

RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\MySoft\\" + SubKey, true);

Adding true to OpenSubKey's function everything work, without problems. Without specifing the parameter "writable", it means that you are going to retrieve a subkey as read-only !!!!!!!!!!!

Code Snippet
public RegistryKey OpenSubKey ( string name, bool writable)

Thanks.

Alex





Re: .NET Base Class Library Write value in Registry. In HKEY_LOCAL_MACHINE

eradicator

Oops, sure wish I would have caught that.