Пример #1
0
        private static List <String> GetSearchDirs(string infFilePath)
        {
            List <String> destinationDirs = IniFileUtils.GetKeys(infFilePath, INF_DestinationDirs);

            List <String> searchDirs = new List <String>();

            foreach (String dir in destinationDirs)
            {
                var dirVal = IniFileUtils.GetValue(infFilePath, INF_DestinationDirs, dir).Split(',');
                var dirid  = int.Parse(dirVal[0]);

                var searchDir = IniFileUtils.ResolveDirId(dirid);
                if (dirVal.Length > 1)
                {
                    searchDir += "\\" + dirVal[1].Trim();
                }

                searchDirs.Add(searchDir);
            }

            return(searchDirs);
        }
Пример #2
0
        /// <summary>
        /// Backs up a driver.
        /// </summary>
        /// <param name="deviceName">The device name whose driver is to be backed up</param>
        /// <param name="infFileName">The name of the driver inf file</param>
        /// <param name="backupDir">The output backup directory</param>
        public bool BackupDriver(string deviceName, string infFileName, string backupDir)
        {
            bool result = false;

            try
            {
                backupDir = backupDir.EndsWith("\\") ? backupDir : backupDir + "\\";
                if (!Directory.Exists(backupDir))
                {
                    Directory.CreateDirectory(backupDir);
                }

                deviceName = deviceName.Trim().Replace('/', ' ').Replace('\\', ' ');
                var deviceBackupDir = backupDir + deviceName + "\\";
                if (!Directory.Exists(deviceBackupDir))
                {
                    Directory.CreateDirectory(deviceBackupDir);
                }

                // Empty target device backup dir
                var oldFiles = new DirectoryInfo(deviceBackupDir).GetFiles();
                foreach (var oldFile in oldFiles)
                {
                    oldFile.Delete();
                }

                var oldDirs = new DirectoryInfo(deviceBackupDir).GetDirectories();
                foreach (var oldDir in oldDirs)
                {
                    oldDir.Delete(true);
                }



                /*********************************************************************************************************************/
                /* information can be found at : http://msdn.microsoft.com/en-us/library/windows/hardware/ff553973%28v=vs.85%29.aspx */
                /*********************************************************************************************************************/

                // Check if driver exists
                String windir          = Environment.GetEnvironmentVariable("windir") + "\\";
                String driverStorePath = windir + "System32\\DriverStore\\FileRepository";

                if (Directory.Exists(driverStorePath))
                {
                    // Driver Store (Windows Vista and Windows Server 2008, Windows 7)

                    String[] possibleDriverDirsInStore = Directory.GetDirectories(driverStorePath, infFileName + "*");
                    if (possibleDriverDirsInStore != null && possibleDriverDirsInStore.Length == 1)
                    {
                        CopyFolder(possibleDriverDirsInStore[0], deviceBackupDir);
                    }
                }
                else
                {
                    // DevicePath (Windows Server 2003, Windows XP, and Windows 2000)

                    RegistryKey currentVersion = Registry.LocalMachine.OpenSubKey(@"SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion");
                    String[]    devicePaths    = currentVersion.GetValue("DevicePath").ToString().Split(';');
                    String      devicePath     = devicePaths[0];

                    // Backup inf file
                    String infFilePath = Path.Combine(devicePath, infFileName);
                    File.Copy(infFilePath, deviceBackupDir + infFileName);

                    // Backup PNF file
                    var pnfFileName = infFileName.Replace(".inf", ".PNF");
                    var pnfFilePath = Path.Combine(devicePath, pnfFileName);
                    File.Copy(pnfFilePath, deviceBackupDir + pnfFileName);

                    // Backup CAT file
                    string originalCATName = IniFileUtils.GetValue(infFilePath, "Version", "CatalogFile");
                    if (!String.IsNullOrEmpty(originalCATName))
                    {
                        var catName     = infFileName.Replace(".inf", ".cat");
                        var catroot     = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\catroot";
                        var catrootDirs = new DirectoryInfo(catroot).GetDirectories();

                        foreach (var dir in catrootDirs)
                        {
                            var catPath = dir.FullName + "\\" + catName;
                            if (File.Exists(catPath))
                            {
                                File.Copy(catPath, deviceBackupDir + originalCATName);
                                break;
                            }
                        }
                    }

                    // backup "layout" file
                    String layoutFile = IniFileUtils.GetValue(infFilePath, "Version", "LayoutFile");
                    if (!String.IsNullOrEmpty(layoutFile))
                    {
                        File.Copy(Path.Combine(devicePath, layoutFile), deviceBackupDir + layoutFile);
                    }


                    // Backup driver files from by parsing the inf file
                    if (Is64BitWindows())
                    {
                        BackupDriverFilesFromInf(".amd64", infFilePath, deviceBackupDir);
                    }
                    else
                    {
                        BackupDriverFilesFromInf(".x86", infFilePath, deviceBackupDir);
                    }
                }
                result = true;
            }
            catch { }

            return(result);
        }
Пример #3
0
        private void BackupDriverFilesFromInf(string platform, string infFilePath, string deviceBackupDir)
        {
            String layoutFilePath = null;

            if (!String.IsNullOrEmpty(IniFileUtils.GetValue(infFilePath, "Version", "LayoutFile")))
            {
                // system-supplied INF
                layoutFilePath = Path.Combine(Path.GetDirectoryName(infFilePath), IniFileUtils.GetValue(infFilePath, "Version", "LayoutFile"));
            }

            String sdf = INF_SourceDisksFiles + platform;
            String sdn = INF_SourceDisksNames + platform;

            // Get driver files
            List <String> driverFiles = GetDriverFiles(platform, layoutFilePath ?? infFilePath, ref sdf);

            // Determine source disks names section
            List <String> sourceDisk = GetDiskNames(platform, layoutFilePath ?? infFilePath, ref sdn);

            // Get search dirs
            List <String> searchDirs = GetSearchDirs(infFilePath);


            //
            foreach (String driverFile in driverFiles)
            {
                /** seems to have a meaning only on windows Vista + **/
                String[] sourceDiskIds = IniFileUtils.GetValue(layoutFilePath ?? infFilePath, sdf, driverFile).Split(',');

                List <String> sourcePath = new List <String>();
                foreach (String sourceDiskId in (from d in sourceDiskIds where !String.IsNullOrEmpty(d) select d))
                {
                    String[] paths = IniFileUtils.GetValue(layoutFilePath ?? infFilePath, sdn, sourceDiskId).Split(',');
                    sourcePath.AddRange((from p in paths where !String.IsNullOrEmpty(p) select p));
                }
                sourcePath = sourcePath.Distinct().ToList();
                sourcePath.Add(""); // add local path


                var backupSubdir = deviceBackupDir;
                if (sourcePath.Count == 4)
                {
                    backupSubdir = Path.Combine(deviceBackupDir, sourcePath[3]);
                }

                if (!Directory.Exists(backupSubdir))
                {
                    Directory.CreateDirectory(backupSubdir);
                }
                /*********************************************************/


                foreach (String possibleDir in searchDirs)
                {
                    if (File.Exists(possibleDir + "\\" + driverFile))
                    {
                        File.Copy(possibleDir + "\\" + driverFile, backupSubdir + "\\" + driverFile);
                        break;
                    }
                }
            }
        }