winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

PackageInformation.cs (2678B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="PackageInformation.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.Management.Configuration.Processor.DSCv3.Helpers
      8 {
      9     using System;
     10     using System.IO;
     11     using Windows.Management.Deployment;
     12 
     13     /// <summary>
     14     /// Contains information about a package.
     15     /// </summary>
     16     internal class PackageInformation
     17     {
     18         private const string DscExecutableFileName = "dsc.exe";
     19 
     20         /// <summary>
     21         /// Initializes a new instance of the <see cref="PackageInformation"/> class.
     22         /// </summary>
     23         /// <param name="familyName">The package family name.</param>
     24         public PackageInformation(string familyName)
     25         {
     26             PackageManager packageManager = new PackageManager();
     27 
     28             var packages = packageManager.FindPackagesForUserWithPackageTypes(null, familyName, PackageTypes.Main);
     29 
     30             if (packages != null)
     31             {
     32                 foreach (var package in packages)
     33                 {
     34                     var packageVersion = package.Id.Version;
     35                     Version version = new Version(packageVersion.Major, packageVersion.Minor, packageVersion.Build, packageVersion.Revision);
     36 
     37                     if (this.Version == null || version > this.Version)
     38                     {
     39                         this.Version = version;
     40                     }
     41                 }
     42             }
     43 
     44             string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
     45             string aliasPath = Path.Combine(localAppData, "Microsoft\\WindowsApps", familyName, DscExecutableFileName);
     46 
     47             if (Path.Exists(aliasPath))
     48             {
     49                 this.AliasPath = aliasPath;
     50             }
     51 
     52             if (this.AliasPath != null && this.Version != null)
     53             {
     54                 this.IsInstalled = true;
     55             }
     56         }
     57 
     58         /// <summary>
     59         /// Gets a value indicating whether the package is installed or not.
     60         /// </summary>
     61         public bool IsInstalled { get; private set; }
     62 
     63         /// <summary>
     64         /// Gets the path to the dsc.exe alias.
     65         /// </summary>
     66         public string? AliasPath { get; private set; }
     67 
     68         /// <summary>
     69         /// Gets the version of the package.
     70         /// </summary>
     71         public Version? Version { get; private set; }
     72     }
     73 }