UnauthorizedAccessException writing to Windows (Vista) directory andwriting to Registry

W

wesley.hs76

Hello,

I've the following two problems. I want to write an inifile to the
Windows directory. And I wanna write variables to the Registry. For
both things I get an UnauthorizedAccessException. This problem started
when I'm using Windows Vista. (Windows XP this problem never happens.)
Can somebody give a solution, how to solve this? This a part of my
source:

static string FBestandnaam;
...
public string Naam
{
...
}
...
public void Reset()
{
if (FFullBestandnaam != null)
Close();
// UnauthorizedAccessException => Here the exception
starts
FFullBestandnaam = File.Open(Naam, FileMode.OpenOrCreate,
FileAccess.ReadWrite);
FFullBestandnaam.Position = 0;
}


private static RegistryKey _Key_LocalMachine =
Registry.LocalMachine;
private static RegistryKey _Key_Software;
private static RegistryKey _Key_Company;
private static RegistryKey _Key;
private static RegistryKey _Subkey;
...
static void InitRegistry()
{
_Key_Software =
_Key_LocalMachine.OpenSubKey(sKEY_SOFTWARE, true);
using (RegistryKey
// UnauthorizedAccessException => Here the exception
starts
tempKey =
_Key_LocalMachine.CreateSubKey(sKEY_SOFTWARE))
{
_Key_Software =
_Key_LocalMachine.OpenSubKey(sKEY_SOFTWARE, true);
}

_Key_Company = _Key_Software.OpenSubKey(sKEY_COMPANY,
true);
using (RegistryKey
tempKey = _Key_Software.CreateSubKey(sKEY_COMPANY))
{
_Key_Company = _Key_Software.OpenSubKey(sKEY_COMPANY,
true);
}
}

Yours sincerly, Wesley
 
P

Peter Bromberg [C# MVP]

Wesley,
This is happening because the Windows Identity your code is running under
does not have the required permissions. Since you haven't provided
information about what kind of application this is or how the credentials
have been determined, the best advice would be to search on "how to run an
application as Administrator".
In particular, Willy DeNoyette has a post earilier in this group about how
to embed the correct manifest for same into your application.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top