/// <summary> /// Import a driver package file (Self-extracting .exe or .zip) /// </summary> private bool ImportPackageFile() { string source = _welcomeControl.PrintDriverPaths.FirstOrDefault(); string destinationDirectory = _completionControl.DestinationPaths.FirstOrDefault(); string destination = Path.Combine(PrintDriversManager.DriverRepositoryLocation, destinationDirectory); try { if (!CheckDestination(destination)) { return(false); } // Unzips the package to the destination server ZipFile.ExtractToDirectory(source, destination); // Read .inf files in the new destination folder to create database entries using (AssetInventoryContext context = DbConnect.AssetInventoryContext()) { //add the x86 and x64 drivers foreach (string fileName in Directory.GetFiles(destination, "*.inf", SearchOption.TopDirectoryOnly)) { using (DriverInfReader reader = new DriverInfReader(fileName)) { DriverInfParser parser = new DriverInfParser(reader); DriverInf inf = parser.BuildInf(); if (inf.DriverClass == "Printer") { //if this is a fax driver, ignore it string driverName = parser.GetDiscreteDriverName(); if (!driverName.Contains("fax", StringComparison.OrdinalIgnoreCase)) { UpdateRepositoryDatabase(inf, driverName, destinationDirectory, context); } } } } context.SaveChanges(); } return(true); } catch (ArgumentOutOfRangeException ex) { TraceFactory.Logger.Error(ex.Message, ex); MessageBox.Show(ex.Message, "Import Package File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } catch (InvalidDataException ex) { TraceFactory.Logger.Error(ex.Message, ex); StringBuilder message = new StringBuilder("Unable to extract:"); message.AppendLine(); message.AppendLine(source); message.AppendLine("Try manually extracting the driver package, then import using 'Driver INF Folder'."); MessageBox.Show(message.ToString(), "Import Package File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } return(false); }
/// <summary> /// Upload the driver to the repository /// </summary> /// <param name="infFile"></param> private void UploadFolder(DriverInf infFile, PrintDriverPackage printDriverPackage) { string discreteDriverRepo = PrintDriversManager.DriverRepositoryLocation; var path = Path.Combine(discreteDriverRepo, printDriverPackage.Name); try { //this will take some time depending upon size of the driver package FileSystem.CopyDirectory(Path.GetDirectoryName(infFile.Location), path); } catch (Exception ex) { MessageBox.Show("Error saving driver {0} to Driver Repository. {1}".FormatWith(printDriverPackage.Name, ex.Message), "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void GetInfFile(string folderPath) { //only searching the top level directory for INF files, the discrete driver has lot of INF files in various directory, which is unnecessary to go through. Collection <DriverInf> files = new Collection <DriverInf>(); foreach (string fileName in Directory.GetFiles(folderPath, "*.inf", SearchOption.TopDirectoryOnly)) { using (DriverInfReader reader = new DriverInfReader(fileName)) { DriverInfParser parser = new DriverInfParser(reader); DriverInf inf = parser.BuildInf(); if (inf.DriverClass == "Printer") { files.Add(inf); } } } switch (files.Count()) { case 0: MessageBox.Show("The selected directory does not contain any print drivers. \nPlease select the directory which has the print driver setup information file (*.inf).", "Print Driver not found", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); break; case 1: location_ListBox.Items.Add(files[0].Location); break; default: MessageBox.Show("The selected directory contains more than one print driver setup information file (*.inf).\nPlease choose the file you want to use.", "Multiple .INF Files Found", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); using (InfPreviewForm previewForm = new InfPreviewForm()) { previewForm.Text = folderPath; previewForm.Initialize(files); if (previewForm.ShowDialog(this) == DialogResult.OK) { location_ListBox.Items.Add(previewForm.SelectedFile); } } break; } }
/// <summary> /// add the driver to the repository /// </summary> /// <param name="driverDirectory"></param> private void AddDiscretePrinterDriver(string driverDirectory) { //only searching the top level directory for INF files, the discrete driver has lot of INF files in various directory, which is unnecessary to go through. if (Directory.GetFiles(driverDirectory, "*.inf", SearchOption.TopDirectoryOnly).Count() == 0) { MessageBox.Show("The selected directory does not contain any print drivers, please select the directory which has the print driver setup information file (*.inf).", "Print Driver not found", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } //add the x86 and x64 drivers foreach (string fileName in Directory.GetFiles(driverDirectory, "*.inf", SearchOption.TopDirectoryOnly)) { using (DriverInfReader reader = new DriverInfReader(fileName)) { DriverInfParser parser = new DriverInfParser(reader); DriverInf inf = parser.BuildInf(); if (inf.DriverClass == "Printer") { string driverName = parser.GetDiscreteDriverName(); UploadDiscreteDriver(inf, driverName); } } } }
/// <summary> /// Import the driver from a pre-installed directory location. /// </summary> private bool ImportFromFolder() { string source = _welcomeControl.PrintDriverPaths.FirstOrDefault(); string destination = _completionControl.DestinationPaths.FirstOrDefault(); var path = Path.Combine(PrintDriversManager.DriverRepositoryLocation, destination); if (!CheckDestination(path)) { return(false); } PrintDriverPackage printDriverPackage = null; using (AssetInventoryContext context = DbConnect.AssetInventoryContext()) { //add the drivers using (DriverInfReader reader = new DriverInfReader(source)) { DriverInfParser parser = new DriverInfParser(reader); DriverInf inf = parser.BuildInf(); string driverName = parser.GetDiscreteDriverName(); //if this is a fax driver, ignore it if (!driverName.Contains("fax", StringComparison.OrdinalIgnoreCase)) { printDriverPackage = UpdateRepositoryDatabase(inf, driverName, destination, context); UploadFolder(inf, printDriverPackage); } } context.SaveChanges(); } return(true); }
private void UploadDiscreteDriver(DriverInf infFile, string driverName) { string discreteDriverRepo = GlobalSettings.Items[Setting.PrintDriverServer]; PrintDriverPackage newDiscretePrintDriverPackage = new PrintDriverPackage(); newDiscretePrintDriverPackage.Name = new DirectoryInfo(Path.GetDirectoryName(infFile.Location)).Name; //if this is a fax driver, ignore it if (driverName.Contains("fax", StringComparison.OrdinalIgnoreCase)) { return; } using (AssetInventoryContext context = DbConnect.AssetInventoryContext()) { //if the driver package already exist just update the x86 or x64 driver inf file var discreteDriver = context.PrintDriverPackages.Include("PrintDrivers").FirstOrDefault(n => n.Name == newDiscretePrintDriverPackage.Name); if (discreteDriver != null) { newDiscretePrintDriverPackage = discreteDriver; if (infFile.Drivers.Any(n => n.Architecture == DriverArchitecture.NTAMD64)) { //passing only the path relative to the repository newDiscretePrintDriverPackage.InfX64 = Path.Combine(newDiscretePrintDriverPackage.Name, Path.GetFileName(infFile.Location)); } if (infFile.Drivers.Any(n => n.Architecture == DriverArchitecture.NTx86)) { //passing only the path relative to the repository newDiscretePrintDriverPackage.InfX86 = Path.Combine(newDiscretePrintDriverPackage.Name, Path.GetFileName(infFile.Location)); } } else { //this is a new upload, add the necessary details newDiscretePrintDriverPackage.PrintDriverPackageId = SequentialGuid.NewGuid(); newDiscretePrintDriverPackage.InfX64 = string.Empty; newDiscretePrintDriverPackage.InfX86 = string.Empty; if (infFile.Drivers.Any(n => n.Architecture == DriverArchitecture.NTAMD64)) { //passing only the path relative to the repository newDiscretePrintDriverPackage.InfX64 = Path.Combine(newDiscretePrintDriverPackage.Name, Path.GetFileName(infFile.Location)); } if (infFile.Drivers.Any(n => n.Architecture == DriverArchitecture.NTx86)) { //passing only the path relative to the repository newDiscretePrintDriverPackage.InfX86 = Path.Combine(newDiscretePrintDriverPackage.Name, Path.GetFileName(infFile.Location)); } //the folder does not exist, add them up to the repository if (Directory.Exists(Path.Combine(discreteDriverRepo, newDiscretePrintDriverPackage.Name))) { TraceFactory.Logger.Debug("Driver Package already exists"); } else { //this will take sometime depending upon size of the driver package FileSystem.CopyDirectory(Path.GetDirectoryName(infFile.Location), Path.Combine(discreteDriverRepo, newDiscretePrintDriverPackage.Name)); } context.PrintDriverPackages.Add(newDiscretePrintDriverPackage); } PrintDriver newdiscreteDriver = null; newdiscreteDriver = context.PrintDrivers.FirstOrDefault(n => n.Name.Equals(driverName)); if (newdiscreteDriver == null || !newdiscreteDriver.PrintDriverPackage.Name.EqualsIgnoreCase(newDiscretePrintDriverPackage.Name)) { newdiscreteDriver = new PrintDriver(); newdiscreteDriver.Name = driverName; newdiscreteDriver.PrintDriverId = SequentialGuid.NewGuid(); newdiscreteDriver.PrintDriverPackage = newDiscretePrintDriverPackage; newdiscreteDriver.PrintProcessor = infFile.Drivers.FirstOrDefault().PrintProcessor; context.PrintDrivers.Add(newdiscreteDriver); } context.SaveChanges(); } }
private PrintDriverPackage UpdateRepositoryDatabase(DriverInf infFile, string driverName, string packageVersion, AssetInventoryContext context) { string driverRepo = PrintDriversManager.DriverRepositoryLocation; PrintDriverPackage printDriverPackage = new PrintDriverPackage(); printDriverPackage.Name = packageVersion; //new DirectoryInfo(Path.GetDirectoryName(infFile.Location)).Name; //if the driver package already exists just update the x86 or x64 driver inf file PrintDriverPackage existingDriverPackage = context.PrintDriverPackages.Include("PrintDrivers").FirstOrDefault(n => n.Name == printDriverPackage.Name); if (existingDriverPackage != null) { printDriverPackage = existingDriverPackage; if (infFile.Drivers.Any(n => n.Architecture == DriverArchitecture.NTAMD64)) { //passing only the path relative to the repository printDriverPackage.InfX64 = Path.Combine(printDriverPackage.Name, Path.GetFileName(infFile.Location)); } if (infFile.Drivers.Any(n => n.Architecture == DriverArchitecture.NTx86)) { //passing only the path relative to the repository printDriverPackage.InfX86 = Path.Combine(printDriverPackage.Name, Path.GetFileName(infFile.Location)); } } else { //this is a new upload, add the necessary details printDriverPackage.PrintDriverPackageId = SequentialGuid.NewGuid(); printDriverPackage.InfX64 = string.Empty; printDriverPackage.InfX86 = string.Empty; if (infFile.Drivers.Any(n => n.Architecture == DriverArchitecture.NTAMD64)) { //passing only the path relative to the repository printDriverPackage.InfX64 = Path.Combine(printDriverPackage.Name, Path.GetFileName(infFile.Location)); } if (infFile.Drivers.Any(n => n.Architecture == DriverArchitecture.NTx86)) { //passing only the path relative to the repository printDriverPackage.InfX86 = Path.Combine(printDriverPackage.Name, Path.GetFileName(infFile.Location)); } context.PrintDriverPackages.Add(printDriverPackage); } PrintDriver newPrintDriver = context.PrintDrivers.FirstOrDefault(n => n.Name.Equals(driverName)); if (newPrintDriver == null || !newPrintDriver.PrintDriverPackage.Name.EqualsIgnoreCase(printDriverPackage.Name)) { newPrintDriver = new PrintDriver(); newPrintDriver.Name = driverName; newPrintDriver.PrintDriverId = SequentialGuid.NewGuid(); newPrintDriver.PrintDriverPackage = printDriverPackage; newPrintDriver.PrintProcessor = infFile.Drivers.FirstOrDefault().PrintProcessor; context.PrintDrivers.Add(newPrintDriver); } return(printDriverPackage); }