Click here to Skip to main content
15,917,971 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to find process(my application which is .exe file hosted on server) running on other user's machine and kill that process. using C# code.

What I have tried:

Below code can find only one user and the process running on that one user's system.

But I want to find given hosted application running on how many users and I want to kill that process.



public string GetProcessOwner(string processName)
        {
            string query = "Select * From Win32_Process Where Name = '" + processName +"'";
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
            ManagementObjectCollection processList = searcher.Get();

            DataTable dt = new DataTable();
            dt.Columns.Add("UserName", typeof(string));
            dt.Columns["UserName"].ReadOnly = true;

            dt.Columns.Add("ProcessName", typeof(string));
            dt.Columns["ProcessName"].ReadOnly = true;

            dt.Columns.Add("ProcessId", typeof(string));
            dt.Columns["ProcessId"].ReadOnly = true;

            txtUserCount.Text = processList.Count.ToString();
            foreach (ManagementObject clsProcess in processList)
            {
                DataRow r = dt.NewRow();
                string[] argList = new string[] { string.Empty, string.Empty };
                int returnVal = Convert.ToInt32(clsProcess.InvokeMethod("GetOwner", argList));
                if (returnVal == 0)
                {
                    // return DOMAIN\user
                    r["UserName"] = argList[1] + "\\" + argList[0];
                    r["ProcessName"] = (String)clsProcess.GetPropertyValue("Name");
                    r["ProcessId"] = (String)clsProcess.GetPropertyValue("Handle");

                    dt.Rows.Add(r);
                }

            }
            dgvAppUsers.DataSource = dt;
            return "NO OWNER";
        }
Posted

If I am understanding you correctly, you want to find one app from another and close it.

I have written an article called Silent ClickOnce Installer for Winform & WPF in C# & VB[^]. There is a class named AppProcessHelper that can identify if the app is already running and close itself. The same concept can be used.
 
Share this answer
 
Unless you have admin permissions on that machine, you aren't going to be able to kill the process from the server - you don't own it, the user who started it does - where the EXE file was located is irrelevant once the process it started.

What are you trying to do that you think that might be a good solution?
 
Share this answer
 
Comments
Meena_16237389 17-Apr-24 7:41am    
Can we at list find process running on how many and which user.
Dave Kreskowiak 17-Apr-24 9:12am    
Again, only if the code you're using is running as an administrator account on the target machine.

Think about it. If it were possible to see all of the processes running on a remote machine, no matter which user started those processes, it would be a nasty security vulnerability.
If I've understood your question correctly, you're hosting your application files on a shared folder on your server, and want to see who has the file open on a different computer.

Doing this manually would involve opening the "shared folders" MMC snap-in, and looking at the "Open Files" folder.

As far as I can see, doing this in code would either involve P/Invoking the NetFileEnum[^] / NetFileClose[^] functions, or using the IADsResource[^] interface. There's a really old VBScript sample of option 2 in the archived Microsoft dev blogs:

How Can I List Open Sessions and Open Files on a Computer? - Scripting Blog [archived][^]
VBScript
Set objConnection = GetObject("WinNT://atl-ws-01/LanmanServer")
Set colResources = objConnection.Resources


For Each objResource in colResources
    Wscript.Echo "Path: " & objResource.Path
    Wscript.Echo "User: " & objResource.User
    Wscript.Echo
Next
Note the comment at the end:
Quote:
We should point out that when we tested these close session and close file scripts they seemed to work just fine on Windows 2000 Server and Windows Server 2003. Results on Windows XP, however, were mixed at best. So, no guarantees; give them a try and see if they work in your environment.

To use this in C#, I believe you would need to use the System.DirectoryServices namespace[^]. There's an example in this StackOverflow post[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900