Ejemplo n.º 1
0
        public bool ExportAllDrivers(string destinationPath)
        {
            try
            {
                DismApi.Initialize(DismLogLevel.LogErrors);

                try
                {
                    using (DismSession session = this.GetSession())
                        using (SafeWaitHandle safeWaitHandle = new SafeWaitHandle(IntPtr.Zero, false))
                        {
                            int hresult = NativeMethods._DismExportDriver(session, destinationPath, safeWaitHandle, null, IntPtr.Zero);

                            if (hresult == unchecked ((int)0x80070006))
                            {
                                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);
        }