 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 4th, 2004, 02:48 AM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
registry permission problems
I want to alow currently login users to have write permission to HKEY LOCAL MACHINE registry path (by default this user have only read permision). I using C# .NET 2003.
My code is:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using Microsoft.Win32;
using System.Security.Permissions;
using System.Security;
....
[RegistryPermissionAttribute(SecurityAction.Assert, Create = "HKEY_LOCAL_MACHINE\\SOFTWARE\\TEST")]
[RegistryPermissionAttribute(SecurityAction.Assert, All = "HKEY_LOCAL_MACHINE\\SOFTWARE\\TEST\\data")]
private void btnAdd_Click(object sender, System.EventArgs e)
{
RegistryPermission f = new RegistryPermissio(RegistryPermissionAccess.AllAcce ss,"HKEY_LOCAL_MACHINE\\SOFTWARE");
f.AddPathLis(RegistryPermissionAccess.AllAccess,"H KEY_LOCAL_MACHINE\\SOFTWARE\\TEST");
RegistryKey LM=Registry.LocalMachine;
RegistryKey SW=LM.OpenSubKey("Software");
RegistryKey mykey=SW.OpenSubKey("TEST",true);//On this point I get exception !!!!
mykey.CreateSubKey("mama");
mykey.SetValue("data","10");
}
|
|

July 5th, 2004, 03:33 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
|
|
I dont think your problems lies with permission code. The permission granting code is alright. The exeption you are getting is perhaps due to the absense of TEST subkey in the Registery.
Nowhere in the code you have any backup for the case where TEST key is missing. Either add code to create TEST subkey first in case it is missing or manually add this key to the registry to get the code running.
Ankur
|
|

July 5th, 2004, 03:55 AM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry Ankur but I manually add this key to registry and all I want to change its value. But dont work?
Are you shure that I dont have any error in my code?
|
|

July 22nd, 2004, 03:51 AM
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I want to access to a remote computer registry using the code below but i get an error: error : An unhandled exception of type "System.UnauthorizedAccessException" occured in system.management.dll, Additional information : Access is denied . Remote access services in both of the pc is enable and both are using WinXP.
using System;
using System.IO;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Security;
[assembly: RegistryPermissionAttribute(SecurityAction.Request Minimum,
Read = @"HKEY_CURRENT_USER\Environment")]
[assembly: SecurityPermissionAttribute(SecurityAction.Request Minimum,
UnmanagedCode = true)]
class RemoteKey
{
static void Main(string[] args)
{
RegistryKey environmentKey;
string remoteName;
// Check that an argument was specified when the
// program was invoked.
if(args.Length == 0)
{
Console.WriteLine("Error: The name of the remote " +
"computer must be specified when the program is "
+ "invoked.");
return;
}
else
{
remoteName = args[0];
}
try
{
// Open HKEY_CURRENT_USER\Environment
// on a remote computer.
environmentKey = RegistryKey.OpenRemoteBaseKey(
RegistryHive.CurrentUser,remoteName).OpenSubKey("E nvironment");
}
catch(IOException e)
{
Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
return;
}
Console.WriteLine("\nThere are {0} values for {1}.",
environmentKey.ValueCount.ToString(), environmentKey.Name);
foreach(string valueName in environmentKey.GetValueNames())
{
Console.WriteLine("{0,-20}: {1}", valueName,
environmentKey.GetValue(valueName).ToString());
}
// Close the registry key.
environmentKey.Close();
}
}
|
|

July 26th, 2004, 07:36 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi Laslo, First of all accept my apologies for leaving the discussion in between.
About the code : as I said the code has no problem but it wont do what you want it to do, that is assign your application the permissions. What I meant to say is that these attributes are used to first QUERY what permissions are available to you in the zone where your app resides and then ASSOCIATE the selected permissions out of available permissions in the zone to your code.
You can choose only from the permissions available to you.
In your case when your application tries to access the remote computerâs registry your code is in LocalIntranet zone and by default no code in the LocalIntranet zone is allowed to access critical resources like registry.
You code is right but you need to make your remote machines entrust the code residing in LocalIntranet zone, if thatâs fissile for you, because that requires running Policy Administration tools like caspol.exe on the remote machines and that opens the doors for just about anything in that zone which is not recommended.
Thatâs all I have on the topic for now but Iâm sure there must be a better way of getting it done. Do update the forum if you find anything new in this regard.
Ankur
|
|

July 26th, 2004, 09:09 AM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for your time Ankur.In the meantime I solve this problem (the problem was to remember http adress of remothing machine on Local machine) using config files in exe directories.
Now I'm almost sure that this kind of problem can't solve by giving certain perrmisions to cureent user under local machine path in Registry Tree.
Any way to config file in solve this prety well.
Thanks again and ewery luck :-)
|
|

May 31st, 2006, 06:22 AM
|
|
Registered User
|
|
Join Date: May 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi
I want to alow currently login users to have write permission to HKEY LOCAL MACHINE registry path (by default this user have only read permision).
please guide me how to achieve it?
Thanking u
Parashuram osekar
|
|

June 7th, 2007, 05:39 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi keenweng .,, i too have same problem un authorisation access it shows warning about that assembly: in code the warning is
'assembly' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'type'. All attributes in this block will be ignored.
If u got a solution , please letme know..... its urgent for me
mail id [email protected]
|
|

June 15th, 2007, 01:35 AM
|
|
Registered User
|
|
Join Date: Jun 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I also got this problem and moved this code outside of my namespace.I think this error may be due to the declaration of the code inside your namespace.just try this.
Cut your code and paste it next to the using declaration statements.I think this will resolve your problem
Quote:
quote:Originally posted by jilly
hi keenweng .,, i too have same problem un authorisation access it shows warning about that assembly: in code the warning is
'assembly' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'type'. All attributes in this block will be ignored.
If u got a solution , please letme know..... its urgent for me
mail id [email protected]
|
|
|

June 18th, 2007, 05:22 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanx , now it didnt give any error, when in running it gives
" Requested registry access not allowed"
i have to read HKEY_LOCAL_MACHINE\Software\\Microsoft\\Windows\\C urrentVersion\\Uninstall
all the subkeys under this key to list the installed applications on that system.
can anyone suggest me a solution.
|
|
 |