Skip to content

Instantly share code, notes, and snippets.

@danieleli
Created February 14, 2022 20:45
Show Gist options
  • Save danieleli/ec6a1eb02bcfb3247bd1af7e0659b095 to your computer and use it in GitHub Desktop.
Save danieleli/ec6a1eb02bcfb3247bd1af7e0659b095 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
namespace WorkstationId
{
class Program
{
static void Main(string[] args)
{
var path = @"SOFTWARE\Citrix\ICA\Session\ClientName";
var id = RegistryHelper.GetValue(path);
Console.WriteLine("WorktationId: " + id);
Console.ReadLine();
}
}
public static class RegistryHelper
{
// GetValue searches both the 32-bit and 64-bit views in the registry
public static string GetValue(string registryPath)
{
var lastSlashIndex = registryPath.LastIndexOf("\\");
var keyName = registryPath.Substring(0, lastSlashIndex);
var valueName = registryPath.Substring(lastSlashIndex + 1);
var key = Registry.LocalMachine.OpenSubKey(keyName, RegistryKeyPermissionCheck.ReadSubTree);
if (key == null)
{
Console.WriteLine("Registry64");
var hklm64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
key = hklm64.OpenSubKey(keyName, RegistryKeyPermissionCheck.ReadSubTree);
}
else
{
Console.WriteLine("Registry.LocalMachine.OpenSubKey");
}
if (key == null)
{
Console.WriteLine("Registry32");
var hklm32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
key = hklm32.OpenSubKey(keyName, RegistryKeyPermissionCheck.ReadSubTree);
}
if (key != null)
{
var workstationId = key.GetValue(valueName);
if (!string.IsNullOrWhiteSpace(workstationId?.ToString()))
{
return workstationId.ToString();
}
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment