C
C#7mo ago
Luna

✅ Detect the user name who started the process

I need to get the user that started the process but I cannot find where to do this
No description
10 Replies
kurumi
kurumi7mo ago
isn't it what are you looking for?
No description
Luna
LunaOP7mo ago
yes, but i wanna get this info via C# code
kurumi
kurumi7mo ago
I see
Luna
LunaOP7mo ago
i forgot to mention the language, lol sorry
Anchy
Anchy7mo ago
Process.MachineName?
Luna
LunaOP7mo ago
nope, already tried that one
Anchy
Anchy7mo ago
what does that give you
Luna
LunaOP7mo ago
"." on every process
Luna
LunaOP6mo ago
lemme check Didn't make this link work Found this solution: Install Nuget package: System.Management
using System.Management;
...
public static string? GetProcessOwner(int processId)
{
string? MethodResult = null;
try
{
string Query = $"SELECT * FROM WIN32_PROCESS WHERE ProcessId = {processId}";

ManagementObjectCollection Processes = new ManagementObjectSearcher(Query).Get();

foreach (ManagementObject Process in Processes.Cast<ManagementObject>())
{
string[] Args = ["", ""];

int ReturnCode = Convert.ToInt32(Process.InvokeMethod("GetOwner", Args));

MethodResult = ReturnCode switch
{
0 => Args[1] + "\\" + Args[0],
_ => "NONE",
};
}

}
catch (Exception ex)
{
MessageBox.Show($"Error geting process owner: \n{ex.Message}");
}
return MethodResult;
}
using System.Management;
...
public static string? GetProcessOwner(int processId)
{
string? MethodResult = null;
try
{
string Query = $"SELECT * FROM WIN32_PROCESS WHERE ProcessId = {processId}";

ManagementObjectCollection Processes = new ManagementObjectSearcher(Query).Get();

foreach (ManagementObject Process in Processes.Cast<ManagementObject>())
{
string[] Args = ["", ""];

int ReturnCode = Convert.ToInt32(Process.InvokeMethod("GetOwner", Args));

MethodResult = ReturnCode switch
{
0 => Args[1] + "\\" + Args[0],
_ => "NONE",
};
}

}
catch (Exception ex)
{
MessageBox.Show($"Error geting process owner: \n{ex.Message}");
}
return MethodResult;
}
Want results from more Discord servers?
Add your server