locked
Kill a process started by another user using C# RRS feed

  • Question

  • Hi,

    I want to close all browsers started by all the loged in users using C#.

    When I use Process.kill(), it closes all browsers started by same user but gives Access Denied Error for browsers stated by other users.

    How can I close all browsers irrespective of number of users loged in.

    Thanks.

    Wednesday, May 23, 2012 9:20 AM

Answers

  • Hi Akshay,

    Is it possible for you to login to the remote machine and turn UAC off? For that you have to go to Control Panel>User Accounts and select the user with which you run the tests. From there you can turn off UAC. Once done you should not get the UAC window next time you login.

    Regards,

    Pankaj


    Utility to run CUIT Ordered Tests for multiple iterations @ http://cuitbatchrunner.codeplex.com/

    • Marked as answer by AkshayGarg Friday, May 25, 2012 12:24 PM
    Thursday, May 24, 2012 10:48 AM

All replies

  • Hi Akshay,

    I am not sure but if you have the credentials for the other users(as referred by you), then you can try using Windows Impersonation and kill the processes as the process owner. This is something from top of my head and there may be better ways to do it.

    Regards,

    Pankaj


    Utility to run CUIT Ordered Tests for multiple iterations @ http://cuitbatchrunner.codeplex.com/

    Wednesday, May 23, 2012 8:40 PM
  • Or run the process as an administrator and you should be able to do this. 

    Please remember to mark the replies as answers if they help.

    Tarun Arora

    Blog: http://geekswithblogs.net/TarunArora  Subscribe in a reader

    Wednesday, May 23, 2012 10:57 PM
  • Thanks Pankaj. User credentials are not known so it can not be used.
    Thursday, May 24, 2012 10:06 AM
  • Thanks Tarun. I want to close all browsers through coded UI test case running at remote machine. Since test cases can not be started in elevated mode so I need to start cmd in elevated mode and execute the taskkill.

    I tried below code, but it opens UAC window then I need way to suppress that.

    ProcessStartInfo process = new ProcessStartInfo("cmd");
    process.UseShellExecute = true;
    process.CreateNoWindow = true;
    process.Verb = "runas";
    Process.Start(process);

    Also I tried below code but it is asking for password in cmd window, and when I password it gives error bad username\password.

    ProcessStartInfo process = new ProcessStartInfo("runas.exe");
    process.UseShellExecute = true;
    process.CreateNoWindow = true;
    process.Arguments = @"/env /user:"+"Administrator" + " cmd";
    Process.Start(process);

    Even if I get this working, I need a way to pass arguments to new cmd window.

    Thursday, May 24, 2012 10:35 AM
  • Hi Akshay,

    Is it possible for you to login to the remote machine and turn UAC off? For that you have to go to Control Panel>User Accounts and select the user with which you run the tests. From there you can turn off UAC. Once done you should not get the UAC window next time you login.

    Regards,

    Pankaj


    Utility to run CUIT Ordered Tests for multiple iterations @ http://cuitbatchrunner.codeplex.com/

    • Marked as answer by AkshayGarg Friday, May 25, 2012 12:24 PM
    Thursday, May 24, 2012 10:48 AM
  • Thanks Pankaj.

    After turning UAC off I used below code to run cmd in admin mode and kill all IEs.

    Process myprocess = new Process();
                    string arg = @"/c taskkill /f";
                    try
                    {
                        foreach (Process p in Process.GetProcessesByName("iexplore"))
                        {
                            arg += " /pid "+p.Id;
                        }
                        ProcessStartInfo process = new ProcessStartInfo("cmd");
                        process.UseShellExecute = true;
                        process.CreateNoWindow = true;
                        process.Verb = "runas";
                        process.Arguments = arg;
                        Process.Start(process);
                    }

    Friday, May 25, 2012 12:26 PM
  • Hi AkshayGarg,

    Thanks for sharing this solution here.

    Best Regards,


    Tony Xiao [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 28, 2012 9:15 AM
    Moderator
  • you can try this.

        using System.Diagnostics;

        public void CloseAllIE()
        {
        foreach (
    Process proc in Process.GetProcessesByName("iexplore"))
        {
        proc.Kill();
        }
        }

    it will close all IE..


    Ankit Verma

    Monday, May 28, 2012 12:22 PM