Exemplo n.º 1
0
        private static List <RunItem> AddWindows10Apps()
        {
            var list = new List <RunItem>();

            using (PowerShell PowerShellInstance = PowerShell.Create())
            {
                // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
                // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
                PowerShellInstance.AddScript("get-appxpackage");

                // invoke execution on the pipeline (collecting output)
                Collection <PSObject> PSOutput = PowerShellInstance.Invoke();
                // use "AddParameter" to add a single parameter to the last command/script on the pipeline.
                //PowerShellInstance.AddParameter("param1", "parameter 1 value!");
                foreach (PSObject outputItem in PSOutput)
                {
                    //TODO: handle/process the output items if required
                    var item = outputItem.BaseObject;


                    var propertyInfo = item.GetType().GetProperty("NonRemovable");
                    var NonRemovable = (bool)propertyInfo.GetValue(item, null);
                    propertyInfo = item.GetType().GetProperty("IsFramework");
                    var IsFramework = (bool)propertyInfo.GetValue(item, null);
                    //var cast = (Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage)

                    if (!NonRemovable && !IsFramework)
                    {
                        var runItem = new RunItem()
                        {
                            Type = ItemType.Win10App
                        };

                        propertyInfo = item.GetType().GetProperty("PackageFamilyName");
                        var packageFamily = (string)propertyInfo.GetValue(item, null);
                        runItem.Command = packageFamily + "!App";

                        propertyInfo = item.GetType().GetProperty("Name");
                        var names = ((string)propertyInfo.GetValue(item, null)).Split('.').ToList();
                        runItem.Name = names.Last();

                        names.Remove(names.Last());
                        runItem.URI = string.Join(".", names.ToArray());

                        propertyInfo = item.GetType().GetProperty("InstallLocation");
                        var installLocation = (string)propertyInfo.GetValue(item, null);
                        var icon            = GetWin10Icon(installLocation + "\\AppxManifest.xml");
                        runItem.IconName       = icon.icon;
                        runItem.IconBackGround = icon.background;
                        list.Add(runItem);
                        //Console.WriteLine(packageFamily);
                    }
                }
            }

            return(list);
        }
Exemplo n.º 2
0
        private static void StartWin10App(RunItem item)
        {
            try
            {
                using PowerShell powerShellInstance = PowerShell.Create();
                var command =
                    item.Command.Replace(@"\\", @"\").Replace(@"{", @"""{")
                    .Replace(@"}",
                             @"}"""); //This adds "" to the GUID. Otherwise we cant start it from powershell
                // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
                // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
                powerShellInstance.AddScript($@"start shell:appsFolder\{command}");

                // invoke execution on the pipeline (collecting output)
                powerShellInstance.Invoke();
                // use "AddParameter" to add a single parameter to the last command/script on the pipeline.
                //PowerShellInstance.AddParameter("param1", "parameter 1 value!");
            }
            catch (Exception e)
            {
                Log.Error(e, $"Could not run win10 app {item.Name}");
            }
        }
Exemplo n.º 3
0
        public static void Start(RunItem item, bool asAdmin, bool openContainingFolder)
        {
            try
            {
                //Fire away. We don't need to hold on to it...
                Task.Run(() =>
                {
                    switch (item.Type)
                    {
                    case ItemType.File:
                        if (asAdmin)
                        {
                            WindowHelper.BringProcessToFrontOrStartIt(item.URI, item.Arguments, asAdmin: true);
                        }
                        else if (openContainingFolder)
                        {
                            string args           = $"/e, /select, \"{item.URI}\"";
                            ProcessStartInfo info = new ProcessStartInfo {
                                FileName = "explorer", Arguments = args
                            };
                            Process.Start(info);
                        }
                        else     //The normal start
                        {
                            WindowHelper.BringProcessToFrontOrStartIt(item.URI, item.Arguments, asAdmin: false);
                        }

                        break;

                    case ItemType.Link:
                        if (asAdmin)
                        {
                            WindowHelper.BringProcessToFrontOrStartIt(item.Command, item.Arguments, asAdmin: true);
                        }
                        else if (openContainingFolder)
                        {
                            string args           = $"/e, /select, \"{item.URI}\"";
                            ProcessStartInfo info = new ProcessStartInfo {
                                FileName = "explorer", Arguments = args
                            };
                            Process.Start(info);
                        }
                        else     //The normal start
                        {
                            WindowHelper.BringProcessToFrontOrStartIt(item.Command, item.Arguments, asAdmin: false);
                        }
                        break;

                    case ItemType.Directory:
                        Process.Start(item.URI);
                        break;

                    case ItemType.Win10App:
                        StartWin10App(item);
                        break;

                    case ItemType.RunDialog:
                        RunFileDlg(IntPtr.Zero, IntPtr.Zero, null, null, null, 0);
                        break;

                    case ItemType.ControlPanelSetting:
                        Process.Start(item.Command);
                        break;

                    case ItemType.TurnOffComputer:
                        Process.Start("shutdown", "/s /t 0");
                        break;

                    case ItemType.RestartComputer:
                        Process.Start("shutdown", "/r /t 0");     // the argument /r is to restart the computer
                        break;

                    case ItemType.LogOffComputer:
                        ExitWindowsEx(0, 0);
                        break;

                    case ItemType.LockComputer:
                        LockWorkStation();
                        break;

                    case ItemType.Hibernate:
                        SetSuspendState(true, true, true);
                        break;

                    case ItemType.Sleep:
                        SetSuspendState(false, true, true);
                        break;
                    }
                });
            }
            catch (Exception e)
            {
                Log.Error(e, "Could not execute this command");
            }
        }
Exemplo n.º 4
0
        public static List <RunItem> AddWindows10AppsBetterWay()
        {
            var win10AppList = AddWindows10Apps();

            var list = new List <RunItem>();

            using (PowerShell PowerShellInstance = PowerShell.Create())
            {
                // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
                // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
                PowerShellInstance.AddScript("Get-StartApps");

                // invoke execution on the pipeline (collecting output)
                Collection <PSObject> PSOutput = PowerShellInstance.Invoke();
                // use "AddParameter" to add a single parameter to the last command/script on the pipeline.
                //PowerShellInstance.AddParameter("param1", "parameter 1 value!");

                var dirs = Directory.GetDirectories("C:\\Program Files\\WindowsApps");
                foreach (PSObject outputItem in PSOutput)
                {
                    //TODO: handle/process the output items if required
                    var item = outputItem.ToString();
                    try
                    {
                        var runItem = new RunItem()
                        {
                            Type = ItemType.Win10App
                        };

                        var split = item.Split('=');
                        var name  = split[1].Split(';')[0];
                        var appID = split[2].Substring(0, split[2].Length - 1);
                        runItem.Command = appID;
                        runItem.Name    = name;
                        var uri   = appID;
                        int count = uri.Count(f => f == '{');

                        if (count > 0)
                        {
                            for (int i = 0; i < count; i++)
                            {
                                var    startIndex  = uri.IndexOf("{", StringComparison.Ordinal);
                                var    endIndex    = uri.IndexOf("}", StringComparison.Ordinal);
                                string guids       = uri.Substring(startIndex + 1, endIndex - startIndex - 1);
                                var    guid        = new Guid(guids);
                                var    knownFolder = KnownFolderFinder.GetFolderFromKnownFolderGUID(guid);
                                if (knownFolder != null)
                                {
                                    uri = uri.Replace($"{{{guids}}}", knownFolder);
                                }
                            }
                        }

                        runItem.URI = uri;
                        if (!File.Exists(runItem.URI))
                        {
                            runItem.URI = "Win 10 app";
                        }


                        //propertyInfo = item.GetType().GetProperty("InstallLocation");
                        //var installLocation = (string)propertyInfo.GetValue(item, null);
                        //var icon = GetWin10Icon(installLocation + "\\AppxManifest.xml");


                        try
                        {
                            var hit = win10AppList.FirstOrDefault(p => p.Command.Contains(uri.Split('!')[0]));
                            if (hit != null)
                            {
                                runItem.IconName       = hit.IconName;
                                runItem.IconBackGround = hit.IconBackGround;
                                runItem.KeyWords.Add(hit.Name);
                                win10AppList.Remove(hit);
                            }
                        }
                        catch (Exception)
                        {
                            Log.Error("Could not att win 10 icon.");
                        }


                        list.Add(runItem);
                    }
                    catch (Exception)
                    {
                        Log.Error($"Could not add win 10 app {item}");
                    }
                }
            }
            list.AddRange(win10AppList);

            return(list);
        }