コード例 #1
0
        public static string GetCurrentEdition(string ospath)
        {
            //
            // Initialize DISM log
            //
            string tempLog = Path.GetTempFileName();

            DismApi.Initialize(DismLogLevel.LogErrorsWarningsInfo, tempLog);

            var session = DismApi.OpenOfflineSession(ospath);

            string edition = null;

            try
            {
                edition = DismApi.GetCurrentEdition(session);
            }
            finally //(Exception ex)
            {
            }

            //
            // Clean DISM
            //
            DismApi.CloseSession(session);
            DismApi.Shutdown();
            File.Delete(tempLog);

            return(edition);
        }
コード例 #2
0
        public static void SetProductKey(string ospath, string productkey)
        {
            //
            // Initialize DISM log
            //
            string tempLog = Path.GetTempFileName();

            DismApi.Initialize(DismLogLevel.LogErrorsWarningsInfo, tempLog);

            var session = DismApi.OpenOfflineSession(ospath);

            try
            {
                DismApi.SetProductKey(session, productkey);
            }
            finally //(Exception ex)
            {
            }

            //
            // Clean DISM
            //
            DismApi.CloseSession(session);
            DismApi.Shutdown();
            File.Delete(tempLog);
        }
コード例 #3
0
        public static void SetTargetEdition(string ospath, string edition, ProgressCallback progressCallback)
        {
            int counter = 0;

            //
            // Initialize DISM log
            //
tryagain:
            string tempLog = Path.GetTempFileName();

            DismApi.Initialize(DismLogLevel.LogErrorsWarningsInfo, tempLog);
            DismSession session = DismApi.OpenOfflineSession(ospath);

            try
            {
                void callback3(DismProgress progress)
                {
                    progressCallback?.Invoke(false, (int)Math.Round((double)progress.Current / progress.Total * 100), "Setting edition " + edition);
                }
                DismApi.SetEdition(session, edition, callback3);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed attempt #" + counter);
                Console.WriteLine(ex.ToString());
                //
                // Clean DISM
                //
                DismApi.CloseSession(session);
                try
                {
                    DismApi.Shutdown();
                }
                catch { }
                if (counter < 3)
                {
                    counter++;
                    goto tryagain;
                }
            }

            //
            // Clean DISM
            //
            try
            {
                DismApi.CloseSession(session);
            }
            catch { }
            DismApi.Shutdown();
        }
コード例 #4
0
        public static void UninstallPEComponents(string ospath, ProgressCallback progressCallback)
        {
            //
            // Initialize DISM log
            //
            string tempLog = Path.GetTempFileName();

            DismApi.Initialize(DismLogLevel.LogErrorsWarningsInfo, tempLog);

            var session  = DismApi.OpenOfflineSession(ospath);
            var packages = DismApi.GetPackages(session);
            List <DismPackage> componentsToRemove = new List <DismPackage>();

            //
            // Queue components we don't need according to our hardcoded list for removal
            //
            foreach (var package in packages)
            {
                if (componentsNotInWinPE.Any(x => package.PackageName.StartsWith(x, StringComparison.InvariantCultureIgnoreCase)))
                {
                    componentsToRemove.Add(package);
                }
            }

            //
            // Remove components
            //
            foreach (var pkg in componentsToRemove)
            {
                try
                {
                    void callback3(DismProgress progress)
                    {
                        progressCallback?.Invoke(false, (int)Math.Round((double)progress.Current / progress.Total * 100), "Removing " + pkg.PackageName);
                    }
                    DismApi.RemovePackageByName(session, pkg.PackageName, callback3);
                }
                catch //(Exception ex)
                {
                }
            }

            //
            // Clean DISM
            //
            DismApi.CloseSession(session);
            DismApi.Shutdown();
            File.Delete(tempLog);
        }
コード例 #5
0
        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);
                }
            }
        }