CASA/c_micasad/common/WinUser.cs
2005-10-17 19:43:06 +00:00

101 lines
2.9 KiB
C#

using System;
using System.Collections;
using System.IO;
using Microsoft.Win32;
using sscs.cache;
using sscs.common;
using sscs.constants;
namespace sscs.common
{
internal class WinUser : User
{
private string m_sUserHome = "";
internal WinUser()
{
}
internal WinUser(UserIdentifier winUserId)
{
userId = winUserId;
secretStore = new SecretStore(this);
}
override internal void SetUserName(string username)
{
userName = username;
}
override internal string GetUserName()
{
return userName;
}
/* A method to find the user's home dir on windows needs to be added.
*/
override internal string GetUserHomeDir()
{
CSSSLogger.DbgLog("WinUser:GetUserHomeDir - Entered");
if (m_sUserHome == null || m_sUserHome.Length < 1)
{
CSSSLogger.DbgLog("WinUser:GetUserHomeDir is empty");
//Console.WriteLine("read registry");
// get the users home drive and homepath from the registry
//
string sSIDString = ((WinUserIdentifier)userId).GetSID();
// look up Profile path
// [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-1757981266-436374069-725345543-1006]
CSSSLogger.DbgLog("Reading Reg: SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\" + sSIDString);
string sProfile = ReadRegKey(Registry.LocalMachine, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\" + sSIDString, "ProfileImagePath");
if (sProfile == null)
CSSSLogger.DbgLog("WinUser:GetUserHomeDir get Profile return null");
else
m_sUserHome = sProfile;
//string sHomeDrive = ReadRegKey(Registry.Users, sSIDString+"\\Volatile Environment", "HOMEDRIVE");
//string sHomeDir = ReadRegKey(Registry.Users, sSIDString+"\\Volatile Environment", "HOMEPATH");
//m_sUserHome = sHomeDrive+sHomeDir;
//Console.WriteLine("Homedir: "+ m_sUserHome);
}
CSSSLogger.DbgLog("WinUser:GetUserHomeDir - Exited: "+m_sUserHome);
return m_sUserHome;
}
private string ReadRegKey(RegistryKey rk, string sSubKey, string KeyName)
{
// Opening the registry key
// RegistryKey rk = Registry.Users;
// Open a subKey as read-only
RegistryKey sk1 = rk.OpenSubKey(sSubKey);
// If the RegistrySubKey doesn't exist -> (null)
if ( sk1 == null )
{
return null;
}
else
{
try
{
// If the RegistryKey exists I get its value
// or null is returned.
return (string)sk1.GetValue(KeyName.ToUpper());
}
catch (Exception e)
{
//ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
return null;
}
}
}
}
}