Ejemplo n.º 1
0
        public static async Task <DismFeatureInfo> GetWindowsFeatureInfo(string featureName)
        {
            DismApi.Initialize(DismLogLevel.LogErrors);

            DismFeatureInfo retVal;

            try
            {
                using (var session = DismApi.OpenOnlineSession())
                {
                    retVal = DismApi.GetFeatureInfo(session, featureName);
                }
            }
            finally
            {
                DismApi.Shutdown();
            }

            await Task.Delay(200).ConfigureAwait(false);

            return(retVal);
        }
Ejemplo n.º 2
0
        public override bool ShouldPerformTreatment()
        {
            try
            {
                using (DismSession session = DismApi.OpenOnlineSession())
                {
                    DismFeatureInfo info = DismApi.GetFeatureInfo(session, resolvedFeatureName);
                    if (
                        info.FeatureState == DismPackageFeatureState.NotPresent ||
                        info.FeatureState == DismPackageFeatureState.Removed ||
                        info.FeatureState == DismPackageFeatureState.Staged ||
                        info.FeatureState == DismPackageFeatureState.UninstallPending)
                    {
                        return(false);
                    }
                }
            }
            catch
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        public List <DriverStoreEntry> EnumeratePackages()
        {
            List <DriverStoreEntry> driverStoreEntries = new List <DriverStoreEntry>();

            DismApi.Initialize(DismLogLevel.LogErrors);

            try
            {
                using (DismSession session = DismApi.OpenOnlineSession())
                {
                    foreach (var driverPackage in DismApi.GetDrivers(session, false))
                    {
                        driverStoreEntries.Add(new DriverStoreEntry
                        {
                            DriverClass          = driverPackage.ClassDescription,
                            DriverInfName        = Path.GetFileName(driverPackage.OriginalFileName),
                            DriverPublishedName  = driverPackage.PublishedName,
                            DriverPkgProvider    = driverPackage.ProviderName,
                            DriverSignerName     = driverPackage.DriverSignature.ToString(),
                            DriverDate           = driverPackage.Date,
                            DriverVersion        = driverPackage.Version,
                            DriverFolderLocation = Path.GetDirectoryName(driverPackage.OriginalFileName),
                            DriverSize           = DriverStoreRepository.GetFolderSize(new DirectoryInfo(Path.GetDirectoryName(driverPackage.OriginalFileName))),
                            BootCritical         = driverPackage.BootCritical,
                            Inbox = driverPackage.InBox,
                        });
                    }
                }
            }
            finally
            {
                DismApi.Shutdown();
            }

            return(driverStoreEntries);
        }
Ejemplo n.º 4
0
        public void CheckImageHealthOnlineSessionWithCallback()
        {
            const string expectedUserData = "257E41FC307248608E28C3B1F8D5CF09";

            string userData = null;

            int current = -1;
            int total   = -1;

            using (DismSession session = DismApi.OpenOnlineSession())
            {
                try
                {
                    DismApi.CheckImageHealth(
                        session: session,
                        scanImage: true, // Setting scanImage to true seems to trigger the callback being called
                        progressCallback: progress =>
                    {
                        userData = progress.UserData as string;
                        current  = progress.Current;
                        total    = progress.Total;

                        // Cancel the operation, otherwise it takes ~1 min
                        progress.Cancel = true;
                    },
                        userData: expectedUserData);
                }
                catch (OperationCanceledException)
                {
                }

                userData.ShouldBe(expectedUserData);
                current.ShouldBe(50);
                total.ShouldBe(1000);
            }
        }
Ejemplo n.º 5
0
 public async Task <bool> IsFeatureEnabledAsync(string featureName) =>
 DismApi.GetFeatureInfo(await GetDismSessionAsync(), featureName).FeatureState == DismPackageFeatureState.Installed;
Ejemplo n.º 6
0
 public async Task EnableFeatureAsync(string featureName) =>
 DismApi.EnableFeatureByPackageName(await GetDismSessionAsync(), featureName, null, false, true);
Ejemplo n.º 7
0
        public static async Task <IEnumerable <KeyValuePair <string, DismFeatureInfo> > > GetWindowsFeatureInfo(
            IEnumerable <string> featureNames)
        {
            var enumerable = featureNames as string[] ?? featureNames.ToArray();

            if (!enumerable.Any())
            {
                throw new ArgumentException();
            }

            DismApi.Initialize(DismLogLevel.LogErrors);

            var retVal = new List <KeyValuePair <string, DismFeatureInfo> >();

            try
            {
                using (var session = DismApi.OpenOnlineSession())
                {
                    retVal.AddRange(enumerable.Where(key => !string.IsNullOrWhiteSpace(key))
                                    .Select(featureName => new KeyValuePair <string, DismFeatureInfo>(featureName,
                                                                                                      DismApi.GetFeatureInfo(session, featureName))));
                }
            }
            finally
            {
                DismApi.Shutdown();
            }

            await Task.Delay(200).ConfigureAwait(false);

            return(retVal);
        }
 public override Task InstallPackageAsync(SoftwarePackage package)
 {
     DismApi.EnableFeatureByPackageName(GetDismSession(), package.PackageName, null, false, true);
     return(UpdateCacheAsync(GetCurrentFeatures()));
 }
 private Dictionary <string, DismPackageFeatureState> GetCurrentFeatures() =>
 DismApi.GetFeatures(GetDismSession()).ToDictionary(x => x.FeatureName, x => x.State);
Ejemplo n.º 10
0
        /// <summary>
        ///     Either installs or integrate the update to the live OS or image.
        /// </summary>
        /// <param name="LDR">Install LDR or GDR.</param>
        /// <returns></returns>
        private Status DoWork(Task task, string mountPath, bool LDR)
        {
            if (_progressBar == null)
            {
                _progressBar = new ProgressBar();
            }

            _progressBar.Value = 0;

            Status = Status.Working;

            // if (CheckIntegration(mountPath, LDR)) { return Status.Success; }

            if (task == Task.Install)
            {
                using (var session = DismApi.OpenOnlineSession())
                {
                    DismApi.AddPackage(session, Location, true, false,
                                       delegate(DismProgress progress) { _progressBar.Value = progress.Current; });
                }
                // Processes.Run("pkgmgr.exe", "/ip /m:\"" + Location + "\" /quiet /norestart");
            }
            else
            {
                Processes.Run(DISM.Default.Location,
                              "/Image:\"" + mountPath + "\" /Add-Package /Packagepath:\"" + Location + "\"");
            }

            if (LDR && (_updateType == UpdateType.Unknown || _updateType == UpdateType.LDR))
            {
                if (CheckIntegration(mountPath))
                {
                    return(Status.Failed);
                }
                Extraction.Expand(Location, _tempLocation);

                _updateType = UpdateType.GDR;
                if (File.Exists(_tempLocation + "\\update-bf.mum"))
                {
                    _updateType = UpdateType.LDR;

                    if (task == Task.Install)
                    {
                        Processes.Run("pkgmgr.exe",
                                      "/ip /m:\"" + _tempLocation + "\\update-bf.mum" + "\" /quiet /norestart");
                    }
                    else
                    {
                        var s = Processes.Run("DISM",
                                              "/Image:\"" + mountPath + "\" /Add-Package /Packagepath:\"" + _tempLocation +
                                              "\\update-bf.mum" + "\"");
                        MessageBox.Show(s);
                    }
                }

                FileHandling.DeleteDirectory(_tempLocation);
            }

            UpdateCache.Add(this);

            if (_updateType != UpdateType.LDR)
            {
                LDR = false;
            }

            if (CheckIntegration(mountPath, LDR))
            {
                return(Status.Success);
            }
            return(Status.Failed);
        }
Ejemplo n.º 11
0
        private void integrate_wim()
        {
            //block buttons
            en_dis_able_everything(false);
            //clear progress bar
            refresh_progress_bar_total(0, 100);
            //status bar
            refresh_status_label("Status", "Busy");

            //not necessary, but looks more clear
            List <string> path     = new List <string>();
            List <string> vol      = new List <string>();
            List <string> name     = new List <string>();
            List <string> platform = new List <string>();
            List <string> lang     = new List <string>();
            List <string> ver      = new List <string>();

            for (int i = 0; i < lv_ptr.Items.Count; i++)
            {
                path.Add(lv_ptr.Items[i].SubItems[0].Text);
                vol.Add(lv_ptr.Items[i].SubItems[1].Text);
                name.Add(lv_ptr.Items[i].SubItems[2].Text);
                platform.Add(lv_ptr.Items[i].SubItems[3].Text);
                lang.Add(lv_ptr.Items[i].SubItems[4].Text);
                ver.Add(lv_ptr.Items[i].SubItems[5].Text);
            }

            if (this.textBox_mnt_point.Text.Length == 0)
            {
                mount_point = Environment.GetEnvironmentVariable("tmp") + "\\wim_integrator_mnt_point";
            }
            else
            {
                mount_point = this.textBox_mnt_point.Text;
            }
            if (this.textBox_tmp_folder.Text.Length == 0)
            {
                tmp_folder = des_wim_path + ".temp";
            }
            else
            {
                tmp_folder = this.textBox_tmp_folder.Text;
            }

            if (!Directory.Exists(mount_point))
            {
                Directory.CreateDirectory(mount_point);
            }

            if (!Directory.Exists(tmp_folder))
            {
                Directory.CreateDirectory(tmp_folder);
            }

            DismProgressCallback prog_callback = refresh_progress_bar_step;

            DismApi.Initialize(DismLogLevel.LogErrors);

            //main loop
            for (int i = 0; i < lv_ptr.Items.Count; i++)
            {
                src_wim_path = path[i];
                src_wim_vol  = Convert.ToInt32(vol[i]);
                string vol_name = name[i] + "_" +
                                  platform[i] + "_" +
                                  lang[i] + "_" +
                                  ver[i];

                //mount vol

                //clear progress bar
                refresh_progress_bar_step(0, 100);
                //status bar
                refresh_status_label("DISM", "Mounting");

                lv_ptr.Items[i].BackColor = Color.Gold;//color
                DismApi.MountImage(src_wim_path, mount_point, src_wim_vol, true, prog_callback);

                //imagex
                lv_ptr.Items[i].BackColor = Color.Aqua;//color
                Process imagex = new Process();
                imagex.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                imagex.StartInfo.CreateNoWindow         = true;
                imagex.StartInfo.UseShellExecute        = false;
                imagex.StartInfo.RedirectStandardOutput = true;
                imagex.StartInfo.RedirectStandardError  = true;

                string imagex_flag = "/scroll /compress" + " " + comp_lv + " " + "/temp" + " " + "\"" + tmp_folder + "\"" + " ";
                //imagex cannot create new image by using append, must use capture insted, everything else is the same, stupid.
                string imagex_operation;
                if (i == 0)
                {
                    imagex_operation = "/capture" + " ";
                }
                else
                {
                    imagex_operation = "/append" + " ";
                }
                string imagex_option = "\"" + mount_point + "\"" + " " + "\"" + des_wim_path + "\"" + " " + "\"" + vol_name + "\"";
                imagex.StartInfo.FileName  = "imagex.exe";
                imagex.StartInfo.Arguments = imagex_flag + imagex_operation + imagex_option;

                imagex.Start();

                //clear progress bar
                refresh_progress_bar_step(0, 100);
                string progress;
                while (!imagex.HasExited)
                {
                    progress = imagex.StandardOutput.ReadLine();
                    if (progress != null)
                    {
                        if (progress.StartsWith("[") && !progress.Contains("ERROR"))
                        {
                            refresh_progress_bar_step(Convert.ToInt32(progress.Substring(2, 3)), 100);
                        }
                        //status bar
                        refresh_status_label("IMAGEX", progress);
                    }
                    //avoid load cpu
                    //since there is no any ReadReady() kind of stuff, I have to use this dirty and easy way
                    Thread.Sleep(10);
                }

                //umount vol

                //clear progress bar
                refresh_progress_bar_step(0, 100);
                //status bar
                refresh_status_label("DISM", "Unmounting");

                lv_ptr.Items[i].BackColor = Color.DodgerBlue;//color
                DismApi.UnmountImage(mount_point, false, prog_callback);

                if (imagex.ExitCode == 0)//color
                {
                    lv_ptr.Items[i].BackColor = Color.Lime;
                }
                else
                {
                    lv_ptr.Items[i].BackColor = Color.Red;
                }

                refresh_progress_bar_total(i + 1, lv_ptr.Items.Count);
            }

            DismApi.Shutdown();
            if (Directory.Exists(mount_point))
            {
                Directory.Delete(mount_point, true);
            }
            if (Directory.Exists(tmp_folder))
            {
                Directory.Delete(tmp_folder, true);
            }

            //unblock buttos
            en_dis_able_everything(true);
            refresh_status_label("Status", "Ready");
        }
Ejemplo n.º 12
0
        private void search_wim()
        {
            //block buttons
            en_dis_able_everything(false);
            //clear progress bar
            refresh_progress_bar_step(0, 100);
            refresh_progress_bar_total(0, 100);
            //status bar
            refresh_status_label("Status", "Busy");

            List <string> wim_file_list = new List <string>();

            wim_file_list = Directory.GetFiles(search_path, search_keyword, SearchOption.AllDirectories).ToList();

            this.listView_vol.BeginUpdate();
            DismApi.Initialize(DismLogLevel.LogErrors);
            this.listView_vol.Items.Clear();
            for (int i = 0; i < wim_file_list.Count; i++)
            {
                DismImageInfoCollection imageInfos = DismApi.GetImageInfo(wim_file_list[i]);
                for (int j = 0; j < imageInfos.Count; j++)
                {
                    ListViewItem item_buff = new ListViewItem(wim_file_list[i]);

                    //vol
                    item_buff.SubItems.Add(imageInfos[j].ImageIndex.ToString());

                    //name
                    item_buff.SubItems.Add(imageInfos[j].ImageName);

                    //platform
                    string platform_translated;
                    switch (imageInfos[j].Architecture)
                    {
                    case DismProcessorArchitecture.None:
                        platform_translated = "None";
                        break;

                    case DismProcessorArchitecture.Intel:
                        platform_translated = "x86";
                        break;

                    case DismProcessorArchitecture.AMD64:
                        platform_translated = "amd64";
                        break;

                    case DismProcessorArchitecture.IA64:
                        platform_translated = "ia64";
                        break;

                    case DismProcessorArchitecture.Neutral:
                        platform_translated = "neutral";
                        break;

                    default:
                        platform_translated = "unknow";
                        break;
                    }
                    item_buff.SubItems.Add(platform_translated);

                    //lang
                    item_buff.SubItems.Add(imageInfos[j].DefaultLanguage.Name);

                    //version
                    item_buff.SubItems.Add(imageInfos[j].ProductVersion.ToString());

                    this.listView_vol.Items.Add(item_buff);

                    //progress bar for total index in single file
                    refresh_progress_bar_step(j + 1, imageInfos.Count);
                }
                //progress bar for total file
                refresh_progress_bar_total(i + 1, wim_file_list.Count);
            }
            DismApi.Shutdown();
            this.listView_vol.EndUpdate();
            this.listView_vol.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

            //unblock buttos
            en_dis_able_everything(true);
            refresh_status_label("Status", "Ready");
        }
Ejemplo n.º 13
0
        private void getDriverMountedbutton_Click(object sender, EventArgs e)
        {
            DismApi.Initialize(DismLogLevel.LogErrors);
            Task.Factory.StartNew(() =>
            {
                Task.Run(() =>
                {
                    if (this.InvokeRequired)
                    {
                        this.Invoke((MethodInvoker)(() =>
                        {
                            loadingPanel.Visible = true;
                            mainPanel.Enabled = false;
                            mainPanel.Visible = false;
                        }));
                    }
                    else
                    {
                        loadingPanel.Visible = true;
                        mainPanel.Enabled    = false;
                        mainPanel.Visible    = false;
                    }
                });
                try
                {
                    using (DismSession session = DismApi.OpenOfflineSession(MountPath))
                    {
                        var drivers = DismApi.GetDrivers(session, true);

                        if (this.InvokeRequired)
                        {
                            this.Invoke((MethodInvoker)(() =>
                            {
                                dismOutputListbox.Items.Add("Driver Information");
                                foreach (var driver in drivers)
                                {
                                    dismOutputListbox.Items.Add("Driver: " + driver.ProviderName + " Version: " + driver.Version);
                                }
                            }));
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    if (this.InvokeRequired)
                    {
                        this.Invoke((MethodInvoker)(() =>
                        {
                            loadingPanel.Visible = false;
                            mainPanel.Enabled = true;
                            mainPanel.Visible = true;
                        }));
                    }
                    else
                    {
                        loadingPanel.Visible = false;
                        mainPanel.Enabled    = true;
                        mainPanel.Visible    = true;
                    }
                    DismApi.Shutdown();
                }
            });
        }
Ejemplo n.º 14
0
        private void cleanupOnlineImageButton_Click(object sender, EventArgs e)
        {
            DismApi.Initialize(DismLogLevel.LogErrors);
            Task.Factory.StartNew(() =>
            {
                Task.Run(() =>
                {
                    if (this.InvokeRequired)
                    {
                        this.Invoke((MethodInvoker)(() =>
                        {
                            loadingPanel.Visible = true;
                            mainPanel.Enabled = false;
                            mainPanel.Visible = false;
                        }));
                    }
                    else
                    {
                        loadingPanel.Visible = true;
                        mainPanel.Enabled    = false;
                        mainPanel.Visible    = false;
                    }
                });


                // Initialize the DismApi



                try
                {
                    List <string> sourcePath = new List <string> {
                        @"C:\"
                    };
                    using (DismSession session = DismApi.OpenOnlineSession())
                    {
                        // Get the features of the image
                        Task.Run(() =>
                        {
                            if (this.InvokeRequired)
                            {
                                this.Invoke((MethodInvoker)(() =>
                                {
                                    label1.Text = "Checking Health";
                                }));
                            }
                        });
                        var health = DismApi.CheckImageHealth(session, false, dismProgress_action);
                        Task.Run(() =>
                        {
                            if (this.InvokeRequired)
                            {
                                this.Invoke((MethodInvoker)(() =>
                                {
                                    label1.Text = "Restoring Health";
                                }));
                            }
                        });
                        DismApi.RestoreImageHealth(session, true, null, dismProgress_action);
                    }
                }
                finally
                {
                    if (this.InvokeRequired)
                    {
                        this.Invoke((MethodInvoker)(() =>
                        {
                            loadingPanel.Visible = false;
                            mainPanel.Enabled = true;
                            mainPanel.Visible = true;
                        }));
                    }
                    else
                    {
                        loadingPanel.Visible = false;
                        mainPanel.Enabled    = true;
                        mainPanel.Visible    = true;
                    }
                    DismApi.Shutdown();
                }
            });
        }
Ejemplo n.º 15
0
 private void addDriverMountedButton_Click(object sender, EventArgs e)
 {
     if (driverMountPathTextBox.Text == "PathToDriver" || driverMountPathTextBox.Text == "" || driverMountPathTextBox.Text == null)
     {
         MessageBox.Show("Please Enter PathToDriver");
     }
     else
     {
         DismApi.Initialize(DismLogLevel.LogErrors);
         string driverPath = driverMountPathTextBox.Text;
         Task.Factory.StartNew(() =>
         {
             Task.Run(() =>
             {
                 if (this.InvokeRequired)
                 {
                     this.Invoke((MethodInvoker)(() =>
                     {
                         loadingPanel.Visible = true;
                         mainPanel.Enabled = false;
                         mainPanel.Visible = false;
                     }));
                 }
                 else
                 {
                     loadingPanel.Visible = true;
                     mainPanel.Enabled    = false;
                     mainPanel.Visible    = false;
                 }
             });
             try
             {
                 using (DismSession session = DismApi.OpenOfflineSession(MountPath))
                 {
                     DismApi.AddDriver(session, driverPath, true);
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.ToString());
             }
             finally
             {
                 if (this.InvokeRequired)
                 {
                     this.Invoke((MethodInvoker)(() =>
                     {
                         loadingPanel.Visible = false;
                         mainPanel.Enabled = true;
                         mainPanel.Visible = true;
                     }));
                 }
                 else
                 {
                     loadingPanel.Visible = false;
                     mainPanel.Enabled    = true;
                     mainPanel.Visible    = true;
                 }
                 DismApi.Shutdown();
             }
         });
     }
 }