OpenRemoteBaseKey

D

dm3281

Hello, everyone.

I'm trying to write a C# program in .NET 2.0 that I can use for work to scan
the 90+ in-house programs that we have installed on 100+ servers throughout
our data center. As a result, I wanted a somewhat simple WinForm
application that scanned the remote registry and returned all the
information from the "uninstall" key in the registry.

The main code that I'm using is below.

I have everything working, but I'm struggling with a few things.

1) I'm currently logged into my computer as admin and have the same login on
a remote machine that I want to scan. I would like the option for my
application to prompt the user to use either their current login credentials
(in which the code below would work anyway) or specify an alternate
credential. However, the RemoteRegistry option below doesn' thave any
support for this. So I assume I have to execute some other API call to
establish a secure connection to the remote machine first? If so, how do I
do this?

2) I would also like to check to see if the remote machine exists first
before I attempt to open the registry. This way I don't have to wait 15+
seconds for it to timeout with a network error. Any easy way to do this?

3) I'm trying to develop a 3-tier design. I have the DAL (which the code
below is in), a BAL, which simply gets the entitty object (AppType) back
from the DAL. The BAL would then maniuplate the results and basically
return another entity object back to the UI called (ListViewAppType). I'm
using a ListView to display the data. What is the best way for me to
handle exceptions from the DAL --> BAL --> then UI? I.E., if there is a
network issue or an authentication issue, I want to display a mesage back in
the UI.

4) In the registry under
"Software\Microsoft\Windows\CurrentVersion\Uninstall\", there is a key that
may exist called "DisplayIcon" that typically points to an .EXE file, like
EXPLORER.EXE. If this key exist, how I can extract the icon from this file
to display it in my ListView?

5) In the registry under
"Software\Microsoft\Windows\CurrentVersion\Uninstall\", there is a key that
exist called "InstallDate". The value here is in YYYYMMDD format. Is
there an easy way to convert this to a correct date format, such as
MM/DD/YYYY without doing a lot of string manipulation?


(Most of the code from my DAL:)

using System;
using System.Collections.Generic;
using System.Text;

namespace VersionChecker
{
class AppType
{
private string _DisplayName;

public string DisplayName
{
get { return _DisplayName;}
set { _DisplayName = value;}
}

private string _DisplayVersion;

public string DisplayVersion
{
get { return _DisplayVersion;}
set { _DisplayVersion = value;}
}

private string _Publisher;

public string Publisher
{
get { return _Publisher; }
set { _Publisher = value; }
}

private string _InstallDate;

public string InstallDate
{
get { return _InstallDate; }
set { _InstallDate = value; }
}
}
}



const string _HKLM =
@"Software\Microsoft\Windows\CurrentVersion\Uninstall\";

static private List<AppType> GetRegistry(string remoteName)
{
RegistryKey localMachine;

List<AppType> appTypes = new List<AppType>();

try
{
localMachine =
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,
remoteName).OpenSubKey(_HKLM);

string[] subKeyNames = localMachine.GetSubKeyNames();
foreach (string subKeyName in subKeyNames)
{
RegistryKey subKey =
localMachine.OpenSubKey(subKeyName);

AppType appType = new AppType();

string[] valueNames = subKey.GetValueNames();

if (ValueNameExists(valueNames, "DisplayName"))
{
appType.DisplayName =
subKey.GetValue("DisplayName").ToString();
}
else
{
continue; // if no display name, skip the entire
subkey
}

if (ValueNameExists(valueNames, "DisplayVersion"))
{
appType.DisplayVersion =
subKey.GetValue("DisplayVersion").ToString();
}
else
{
appType.DisplayVersion = "N/A";
}

if (ValueNameExists(valueNames, "Publisher"))
{
appType.Publisher =
subKey.GetValue("Publisher").ToString();
}
else
{
appType.Publisher = "N/A";
}

if (ValueNameExists(valueNames, "InstallDate"))
{
appType.InstallDate =
subKey.GetValue("InstallDate").ToString();
}
else
{
appType.InstallDate = "N/A";
}

// Add item to object list
appTypes.Add(appType);
}
}
catch (IOException e)
{
throw e;
}
catch (Exception e)
{
throw e;
}

return appTypes;
}

static private bool ValueNameExists(string[] valueNames, string
valueName)
{
foreach (string s in valueNames)
{
if (s.ToLower() == valueName.ToLower()) return true;
}
return false;
}
 
H

Hakan Fatih YILDIRIM

1) I'm currently logged into my computer as admin and have the same login on
a remote machine that I want to scan. I would like the option for my
application to prompt the user to use either their current login credentials
(in which the code below would work anyway) or specify an alternate
credential. However, the RemoteRegistry option below doesn' thave any
support for this. So I assume I have to execute some other API call to
establish a secure connection to the remote machine first? If so, how do I
do this?

Maybe you can make a client program that sends the data on the pc to
your pc.. t can be better solution than reaching the registry data
remotely..







2) I would also like to check to see if the remote machine exists first
before I attempt to open the registry. This way I don't have to wait 15+
seconds for it to timeout with a network error. Any easy way to do this?

You can use ping class in net namespace to check out..


Hakan Fatih YILDIRIM
MCP
 
D

dm3281

Thanks. I suppose I could do this. But I have 90+ serers, so this will
definitely be a support nightmare initially until I get everything working
and baked.

I have been using GFILanGuard for quite some time and like how it does its
scans, etc. It installs an agent as well, but I'm not sure what it uses the
agent for.
 

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