public static void ApplyUnattend(string ospath, string unattendpath) { bool ok = false; while (!ok) { try { // // Initialize DISM log // string tempLog = Path.GetTempFileName(); DismApi.Initialize(DismLogLevel.LogErrorsWarningsInfo, tempLog); var session = DismApi.OpenOfflineSession(ospath); try { DismApi.ApplyUnattend(session, unattendpath, true); } finally //(Exception ex) { } // // Clean DISM // DismApi.CloseSession(session); DismApi.Shutdown(); File.Delete(tempLog); ok = true; } catch (Exception ex) { Console.WriteLine("Failed applying unattend, retrying in one second..."); Console.WriteLine(ex.ToString()); ok = false; Thread.Sleep(1000); } } }
public bool ExportAllDrivers(string destinationPath) { try { DismApi.Initialize(DismLogLevel.LogErrors); try { using (DismSession session = this.GetSession()) { int hresult = NativeMethods._DismExportDriver(session, destinationPath); if (hresult != 0 && hresult != 1) { string lastErrorMessage = DismApi.GetLastErrorMessage(); if (!string.IsNullOrEmpty(lastErrorMessage)) { throw new DismException(lastErrorMessage.Trim()); } throw new DismException(hresult); } } } finally { DismApi.Shutdown(); } } catch (DismRebootRequiredException) { return(true); } catch (DismException ex) { Trace.TraceError(ex.ToString()); return(false); } return(true); }
private async Task <DismSession> GetDismSessionAsync() { if (disposed) { throw new ObjectDisposedException(nameof(WindowsFeatureManager)); } if (session == null) { await operatingSystem.ElevateAsync(); DismApi.Initialize(DismLogLevel.LogErrors); session = DismApi.OpenOnlineSessionEx(new DismSessionOptions { ThrowExceptionOnRebootRequired = false }); } return(session); }
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); }
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); }
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"); }
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"); }
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(); } }); }
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(); } }); }
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(); } }); } }