public static int GetProcessAffinityMask(string processName = null)
{
Process myProcess = GetProcessByName(ref processName);
int processorAffinity = (int)myProcess.ProcessorAffinity;
Console.WriteLine("Process {0} Affinity Mask is : {1}", processName, FormatAffinity(processorAffinity));
return processorAffinity;
}
public static Process GetProcessByName(ref string processName)
{
Process myProcess;
if (string.IsNullOrEmpty(processName))
{
myProcess = Process.GetCurrentProcess();
processName = myProcess.ProcessName;
}
else
{
Process[] processList = Process.GetProcessesByName(processName);
myProcess = processList[0];
}
return myProcess;
}
private static string FormatAffinity(int affinity)
{
return Convert.ToString(affinity, 2).PadLeft(Environment.ProcessorCount, '0');
}
}
Example of usage :
private static void Main(string[] args)
{
GetProcessAffinityMask();
Console.ReadKey();
}
// Output:
// Process Test.vshost Affinity Mask is : 11111111