473,563 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing registry permissions using c#

Hello

I need to programmaticall y 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 15069
I need to programmaticall y 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.Director yServices when running XP or higher.
As a sample, following retrieves a DACL from a registry key.

using System;
using System.Director yServices;
using System.Runtime. InteropServices ;

// Use ADsSecurityUtil ityClass 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_MA CHINE\SOFTWARE\ Microsoft";
SecurityDescrip tor sd = null;
AccessControlLi st dacl = null;
ADsSecurityUtil ityClass asu = new ADsSecurityUtil ityClass();
// Get DACL Group and OWNER info
asu.SecurityMas k = (int)(ADS_SECUR ITY_INFO_ENUM.A DS_SECURITY_INF O_DACL) |
(int)(ADS_SECUR ITY_INFO_ENUM.A DS_SECURITY_INF O_GROUP)|
(int)(ADS_SECUR ITY_INFO_ENUM.A DS_SECURITY_INF O_OWNER);
try {
sd = asu.GetSecurity Descriptor(regP ath,
(int)ADS_PATHTY PE_ENUM.ADS_PAT H_REGISTRY,
(int)ADS_SD_FOR MAT_ENUM.ADS_SD _FORMAT_IID) as SecurityDescrip tor;
}
catch(COMExcept ion ce)
{
// Be sure logon user has access to local/remote system
Console.WriteLi ne(ce.Message);
return;
}
// Get DACL from SD
dacl = sd.Discretionar yAcl as AccessControlLi st;
if (dacl != null) {
Console.WriteLi ne("Control: {0}", sd.Control);
Console.WriteLi ne("Owner: {0}", sd.Owner);
Console.WriteLi ne("Group: {0}", sd.Group);
Console.WriteLi ne("Revision: {0}", sd.Revision);
DumpDacl(dacl);
}
}
static void DumpDacl(IADsAc cessControlList dacl)
{
IADsAccessContr olEntry ace = null;
Console.WriteLi ne("------- No. of ACE's {0}-----------", dacl.AceCount);
foreach(object ac in dacl) {
ace = ac as IADsAccessContr olEntry;
Console.WriteLi ne("Access : {0}", Enum.Format(typ eof(ADS_RIGHTS_ ENUM),
ace.AccessMask, "F"));
Console.WriteLi ne(ace.Trustee) ;
Console.WriteLi ne("ACE flags {0}",
Enum.Format(typ eof(ADS_ACEFLAG _ENUM),ace.AceF lags, "x"));
Console.WriteLi ne("ACE type: {0}",
((ADS_ACETYPE_E NUM)ace.AceType ).ToString());
Console.WriteLi ne("----------------");
}
}
}
//--------------
2. Use System.Manageme nt on W2K or higher
3. Use PInvoke interop to use Win32 API's.

Willy.

"Ashok" <as******@hotma il.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Hello

I need to programmaticall y 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_MACH INE
part of the registry. i get the error message:

"Access to the registry key HKEY_LOCAL_MACH INE\Software\te st 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.LocalM achine;
regKey.CreateSu bKey ( thisOne );
regKey = Registry.LocalM achine.OpenSubK ey ( thisOne, true );
regKey.SetValue ( "", thisValue );
regKey.Flush ( );
regKey.Close ( );
regKey = null;
}
catch ( Exception e2 )
{
Console.WriteLi ne ( "Error is " + e2.Message.ToSt ring ( ) );
}
..
..
..

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
1923
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), I am getting a System.Security.SecurityException: Requested registry access is not allowed. The key is there, I exported it from my XP machine. ...
0
7683
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 WMI but the only one of these that goes any where near registry permissions is WMI and that is only to get the permissions of a key (cannot set). I...
1
3035
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 worked just fine until some users were set up as power-users. I've tried to change the permissions on that key programatically, on the fly, but to no...
1
2085
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 application is being installed on with read privilege automatically. Does anyone know of a way to easily set registry permissions? I would prefer to use .NET...
8
5473
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
9088
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 permissions on the keys. How can I accomplish this through code?
1
6546
by: PiotrKolodziej | last post by:
Hi Here is the code: this.regPath = @"Software\FileManager\" ; System.Security.Permissions.RegistryPermission permissions = new System.Security.Permissions.RegistryPermission(System.Security.Permissions.RegistryPermissionAccess.AllAccess, @"HKEY_LOCAL_MACHINE\" + regPath);
3
2277
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 the registry. I know there are applications out there that can read/write to the registry w/o Admin priveleges... is there some way to code around...
0
3998
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, but my department has a couple of web servers of our own that are part of my organization's domain. I am trying to read LDAP information on my...
0
7580
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7882
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6244
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5481
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.