0

I want to list out all user data sources using c# but I can't do it because I don't have permission to read the registry key. How can I get all user data sources?

I tried below code but no use

private List<string> ENUMDSN()
{
    List<string> list = new List<string>();
    list.AddRange(ENUMDSN(Registry.CurrentUser));
    list.AddRange(ENUMDSN(Registry.LocalMachine));
    XElement xmlele = new XElement("list", list.Select(i => new XElement("list", i)));       
    return list;

}

private IEnumerable<string> ENUMDSN(RegistryKey rootkey)
{        
    RegistryKey regkey = rootkey.OpenSubKey(@"Software\ODBC\ODBC.INI\ODBC Data Sources");

    foreach (string name in regkey.GetValueNames())
    {
        string value = regkey.GetValue(name, "").ToString();
        yield return name;
    }
}
Vogel612
  • 5,620
  • 5
  • 48
  • 73
jai
  • 1
  • 1

1 Answers1

-1

"CurrentUser" registry keys doesn't need any specific permission you can read/write without any problem, but if you want to read keys which in the "LocalMachine" subkey you have to have administrator permissions, there is no other way.

EDIT: If you run your code under IIS you cannot get key values under the "CurrentUser" or "LocalMachine" because IIS Application Pools doesn't run under your "current account" they runs under their "user accounts".

For solution maybe you can change your Application Pool's user account but it's not recommended for some security reasons.

iBener
  • 469
  • 6
  • 18
  • actually am getting user data sources when i run my application in local system but i am unable to get from iis and am getting null in place of registry key – jai Jan 12 '15 at 10:37
  • yes you can get a null list because in IIS application pools runs under different user account not your user account. – iBener Jan 12 '15 at 11:10
  • how can i run my application on my user account – jai Jan 12 '15 at 11:34
  • open your IIS Manager and follow the steps under the 'To specify an identity for an application pool': http://technet.microsoft.com/en-us/library/cc771170%28v=ws.10%29.aspx – iBener Jan 12 '15 at 11:53
  • is there any another way to get data sources in c#. – jai Jan 19 '15 at 05:18