简体   繁体   中英

How to access apps with windows and close them in uwp

In wpf you can acess the apps with windows and you are able to close them if you have the id.

Example of code from wpf the method that get the apps

private List<Process> GetProcessesWithWindow()
    {
        List<Process> processwithwindow = new List<Process>();

        Process[] processes = Process.GetProcesses();

        foreach (Process p in processes)
        {
            if (!String.IsNullOrEmpty(p.MainWindowTitle))
            {
                processwithwindow.Add(p);
            }
        }
        return processwithwindow;

    }
}

}

Now the one that block them

        Task Blocker = new Task(() =>
            {
                while (M != minutes)
                {
                    processwithwindow = GetProcessesWithWindow();

                    foreach (Process p in processwithwindow)
                    {

                    if (Usefulprograms.Any(program => program.Id == p.Id ) == false)
                        {
                             p.Kill();
                        }

                    }
                };
            }
            );

There it's a way to achieve this in uwp? I seek and the only thing that I found it's this api https://blogs.windows.com/windowsdeveloper/2017/06/28/uwp-app-diagnostics/ But that only allow me to access to uwp apps and I can't get properties as mainwindotitle or method like kill.

There it's a way to achieve this in uwp?

No, there is no way to do that in UWP apps. UWP apps are running in the sandbox that is isolated from the system. So UWP apps can't directly do actions to the system processes. The UWP App Diagnostics API also shows that you could only get information about the processes.

There it's a way to achieve this in uwp?

Not directly as the app runs in a security constrained sandbox with limited privileges. This is by design for UWP apps.

You could however add a full-trust desktop extension component your UWP app that runs the GetProcessesWithWindow() method. Please refer to Stefan Wick's blog for more information and examples.

The most convinient way that I found to solve the solutions was migrate the project to Win UI 3, why WinUi3 and not WPF, most of the controls and namepaces in WinUI3 are similar to UWP and wpf don´t have some controls that WinUi3 and UWP have

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM