473,461 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Changing registry permissions using c#

Hello

I need to programmatically change the permissions (ACL) on a specific
registry key in a .NET app. Is there a way to do this in .NET?
Thanks for the help,
Ashok

Nov 16 '05 #1
4 15049
I need to programmatically change the permissions (ACL) on a specific
registry key in a .NET app. Is there a way to do this in .NET?


You use the same Win32 API functions you would in an unmanaged
application.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Three options:
1.Use System.DirectoryServices when running XP or higher.
As a sample, following retrieves a DACL from a registry key.

using System;
using System.DirectoryServices;
using System.Runtime.InteropServices;

// Use ADsSecurityUtilityClass available on XP and higher (activeds.dll)
// Interop Assembly created with tlbimp.exe from activeds.tlb,
// or by setting a reference to the typelib from within the IDE
using activedsnet;

class Tester {
public static void Main() {
// Local registry object
string regPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft";
SecurityDescriptor sd = null;
AccessControlList dacl = null;
ADsSecurityUtilityClass asu = new ADsSecurityUtilityClass();
// Get DACL Group and OWNER info
asu.SecurityMask = (int)(ADS_SECURITY_INFO_ENUM.ADS_SECURITY_INFO_DAC L) |
(int)(ADS_SECURITY_INFO_ENUM.ADS_SECURITY_INFO_GRO UP)|
(int)(ADS_SECURITY_INFO_ENUM.ADS_SECURITY_INFO_OWN ER);
try {
sd = asu.GetSecurityDescriptor(regPath,
(int)ADS_PATHTYPE_ENUM.ADS_PATH_REGISTRY,
(int)ADS_SD_FORMAT_ENUM.ADS_SD_FORMAT_IID) as SecurityDescriptor;
}
catch(COMException ce)
{
// Be sure logon user has access to local/remote system
Console.WriteLine(ce.Message);
return;
}
// Get DACL from SD
dacl = sd.DiscretionaryAcl as AccessControlList;
if (dacl != null) {
Console.WriteLine("Control: {0}", sd.Control);
Console.WriteLine("Owner: {0}", sd.Owner);
Console.WriteLine("Group: {0}", sd.Group);
Console.WriteLine("Revision: {0}", sd.Revision);
DumpDacl(dacl);
}
}
static void DumpDacl(IADsAccessControlList dacl)
{
IADsAccessControlEntry ace = null;
Console.WriteLine("------- No. of ACE's {0}-----------", dacl.AceCount);
foreach(object ac in dacl) {
ace = ac as IADsAccessControlEntry;
Console.WriteLine("Access : {0}", Enum.Format(typeof(ADS_RIGHTS_ENUM),
ace.AccessMask, "F"));
Console.WriteLine(ace.Trustee);
Console.WriteLine("ACE flags {0}",
Enum.Format(typeof(ADS_ACEFLAG_ENUM),ace.AceFlags, "x"));
Console.WriteLine("ACE type: {0}",
((ADS_ACETYPE_ENUM)ace.AceType).ToString());
Console.WriteLine("----------------");
}
}
}
//--------------
2. Use System.Management on W2K or higher
3. Use PInvoke interop to use Win32 API's.

Willy.

"Ashok" <as******@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hello

I need to programmatically change the permissions (ACL) on a specific
registry key in a .NET app. Is there a way to do this in .NET?
Thanks for the help,
Ashok

Nov 16 '05 #3

hi,

i have a web service that is trying to access the HKEY_LOCAL_MACHINE
part of the registry. i get the error message:

"Access to the registry key HKEY_LOCAL_MACHINE\Software\test is denied."

i have administrative rights, and its on my machine. the code looks like:

..
..
..
using Microsoft.Win32;
..
..
..
try
{
string thisOne = @"Software\test";
string thisValue = "testing";

RegistryKey regKey = Registry.LocalMachine;
regKey.CreateSubKey ( thisOne );
regKey = Registry.LocalMachine.OpenSubKey ( thisOne, true );
regKey.SetValue ( "", thisValue );
regKey.Flush ( );
regKey.Close ( );
regKey = null;
}
catch ( Exception e2 )
{
Console.WriteLine ( "Error is " + e2.Message.ToString ( ) );
}
..
..
..

what have i forgotten?

regards,
topdog

....had this been a real emergency,
we would have all fled away in terror;
and you would have NOT been notified.

Nov 16 '05 #4
Michael,
what have i forgotten?


That the web service code runs under a different account (i.e. it
doesn't matter if _you_ are an admin).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Malter | last post by:
On my XP Professional dev machine, I am able to read the registry by giving permissions to the ASP.NET account on the local machine. When I transfer the code to my staging server (Windows 2003),...
0
by: DJP | last post by:
Hi there I need to be able to programmatically set permissions on registry keys using VB / VBScript / VBA. So far I have had a look at doing this using the WScript.Shell object, API calls, and...
1
by: Daniel Passwater via DotNetMonster.com | last post by:
I'm working on a app that displays according to a registry key value. The user needs the ability to update the display, and hense the registry key (via a click event on a popup menu). This all...
1
by: Kevin Burton | last post by:
I am using aspnet_setreg but the permissions that it sets the registry to leave the application unable to access the information. I want to add the ASPNET account on the machine that our...
8
by: Al Kaufman | last post by:
I have a simple console app that uses: regSubKey = <some registry key> Dim reg As RegistryKey = Registry.ClassesRoot.OpenSubKey(regSubKey) Dim path As String path = CStr(reg.GetValue(""))
4
by: Kevin L | last post by:
I store some application settings in the registry under HKEY_LOCAL_MACHINE\Software\MyApplication I want to allow full access to this key and subkeys. Currently, I manually change the...
1
by: PiotrKolodziej | last post by:
Hi Here is the code: this.regPath = @"Software\FileManager\" ; System.Security.Permissions.RegistryPermission permissions = new...
3
by: Sreppohcdoow | last post by:
I have a simple tool that I create that stores some settings in the Registry... however, if the user running it doesn't have Admin priveleges on the machine, the app fails when trying to write to...
0
by: tmsprowl | last post by:
Greetings! I was wondering if someone could help me with a problem I'm having. My department is just one of many within my organization. My organization has control over the network domain,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.