CASA/c_micasad/common/WinUser.cs

93 lines
2.4 KiB
C#
Raw Normal View History

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()
{
if (m_sUserHome.Length < 1)
{
//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]
string sProfile = ReadRegKey(Registry.LocalMachine, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\" + sSIDString, "ProfileImagePath");
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);
}
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;
}
}
}
}
}