C
C#2mo 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
kurumi2mo ago
isn't it what are you looking for?
No description
Luna
Luna2mo ago
yes, but i wanna get this info via C# code
kurumi
kurumi2mo ago
I see
Luna
Luna2mo ago
i forgot to mention the language, lol sorry
Anchy
Anchy2mo ago
Process.MachineName?
Luna
Luna2mo ago
nope, already tried that one
Anchy
Anchy2mo ago
what does that give you
Luna
Luna2mo ago
"." on every process
Luna
Luna2mo 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
More Posts