private static IEnumerable <DriverDetails> LoadDrivers(string environment, DriverArchitecture architecture) { string driversRegistryPath = @"SYSTEM\CurrentControlSet\Control\Print\Environments\{0}\Drivers\Version-3"; List <DriverDetails> drivers = new List <DriverDetails>(); using (RegistryKey key = Registry.LocalMachine.OpenSubKey(string.Format(driversRegistryPath, environment))) { if (key != null) { foreach (string driverName in key.GetSubKeyNames()) { using (RegistryKey driverKey = key.OpenSubKey(driverName)) { // The registry entry for the driver must have the DriverVersion. If not, // we will consider this entry NOT a driver... if (driverKey?.GetValue("DriverVersion") != null) { DriverDetails driver = CreateDriverDetails(driverKey, driverName); driver.Architecture = architecture; drivers.Add(driver); } } } } } return(drivers); }
private void GetDriverProperties(DriverDetails driver, string driverSectionName) { foreach (string dirtyLine in _reader.GetSection(driverSectionName)) { string line = ExpandVariables(dirtyLine); string[] parts = line.Split('='); string propertyName = parts[0].Trim(); string propertyValue = parts[1].Trim(); switch (propertyName) { case "PrintProcessor": // The format of the line will be something like... // PrintProcessor="HPCPP15,hpcpp15.dll". We need to extract // the "HPCPP15" part. driver.PrintProcessor = propertyValue.Trim('"').Split(',')[0]; break; case "DriverFile": driver.DriverFile = propertyValue; break; case "ConfigFile": driver.ConfigurationFile = propertyValue; break; case "HelpFile": driver.HelpFile = propertyValue; break; case "LanguageMonitor": driver.Monitor = propertyValue.Trim('"').Split(',')[0]; break; case "DataFile": driver.DataFile = propertyValue; break; case "DriverVer": string[] versionParts = propertyValue.Split(','); driver.DriverDate = versionParts[0]; driver.Version = new DriverVersion(versionParts[1]); break; case "Provider": driver.Provider = propertyValue.Trim().Trim('"'); break; case "DataSection": // This is for Xerox inf files, they have another section // that provides this information. GetDriverProperties(driver, propertyValue); break; default: // Default is intentionally left blank. break; } } }
/// <summary> /// Installs the specified driver. /// </summary> /// <param name="driver">The <see cref="DriverDetails" /> describing the driver to install.</param> /// <param name="forceInstall">if set to <c>true</c> install the driver even if it is already installed.</param> public static void Install(DriverDetails driver, bool forceInstall) { if (!forceInstall && IsInstalled(driver)) { LogInfo($"Driver {driver} already installed."); } else { InstallDriver(driver); } }
/// <summary> /// Determines whether the specified driver is installed. /// </summary> /// <param name="driver">The <see cref="DriverDetails" /> describing the driver to check.</param> /// <returns><c>true</c> if the specified driver is installed; otherwise, <c>false</c>.</returns> public static bool IsInstalled(DriverDetails driver) { bool driverFound = (from n in DriverController.GetInstalledPrintDrivers() where n.Name.EqualsIgnoreCase(driver.Name) && n.Architecture == driver.Architecture && n.Version == driver.Version && Path.GetFileName(n.InfPath).EqualsIgnoreCase(Path.GetFileName(driver.InfPath)) select n).Any(); bool processorFound = DriverController.GetInstalledPrintProcessors().Contains(driver.PrintProcessor, StringComparer.OrdinalIgnoreCase); return(driverFound && processorFound); }
/// <summary> /// Upgrades the driver for the specified print queue. /// </summary> /// <param name="driver">The <see cref="DriverDetails" /> describing the driver to upgrade to.</param> /// <param name="queueName">The queue name.</param> /// <exception cref="ArgumentNullException"><paramref name="driver" /> is null.</exception> public static void Upgrade(DriverDetails driver, string queueName) { if (driver == null) { throw new ArgumentNullException(nameof(driver)); } LogInfo($"Upgrading driver of {queueName} to {driver}"); string printUICommand = $"/Xs /n \"{queueName}\" DriverName \"{driver.Name}\""; LogDebug($"Invoking PrintUI command: {printUICommand}"); NativeMethods.InvokePrintUI(printUICommand); LogInfo($"Driver '{driver}' installed."); }
private static void InstallDriver(DriverDetails driver) { if (string.IsNullOrEmpty(driver.Name)) { throw new ArgumentException("Driver name must be provided.", nameof(driver)); } if (string.IsNullOrEmpty(driver.InfPath)) { throw new ArgumentException("Driver INF path must be provided.", nameof(driver)); } LogInfo($"Installing driver {driver}"); string architecture = _printUIArchitectures[driver.Architecture]; string printUICommand = $"/ia /f \"{driver.InfPath}\" /m \"{driver.Name}\" /h \"{architecture}\""; LogDebug($"Invoking PrintUI command: {printUICommand}"); NativeMethods.InvokePrintUI(printUICommand); LogInfo($"Driver '{driver}' installed."); }
/// <summary> /// Gets a list of <see cref="DriverDetails" /> from the specified INF section. /// </summary> /// <param name="sectionName">The INF section name.</param> /// <returns>A collection of <see cref="DriverDetails" /> from the INF section.</returns> public IEnumerable <DriverDetails> GetDrivers(string sectionName) { List <DriverDetails> drivers = new List <DriverDetails>(); foreach (string line in _reader.GetSection(sectionName)) { // Each line should be formatted as "driverName=installRule,hardwareID". // We only care about getting the driver name. string[] parts = line.Split('='); string driverName = ExpandVariables(parts[0]).Trim().Trim('"'); string driverSectionName = parts[1].Split(',')[0].Trim(); if (!drivers.Any(n => n.Name == driverName)) { DriverDetails driver = new DriverDetails(); driver.Name = driverName; driver.InfPath = _reader.FileLocation; GetDriverProperties(driver, driverSectionName); GetDriverProperties(driver, "Version"); drivers.Add(driver); } } return(drivers); }
private static DriverDetails CreateDriverDetails(RegistryKey key, string driverName) { DriverDetails driver = new DriverDetails() { Name = driverName, InfPath = (string)key.GetValue("InfPath"), DriverFile = (string)key.GetValue("Driver"), ConfigurationFile = (string)key.GetValue("Configuration File"), HelpFile = (string)key.GetValue("Help File"), DataFile = (string)key.GetValue("Data File"), PrintProcessor = (string)key.GetValue("Print Processor"), Monitor = (string)key.GetValue("Monitor"), Provider = (string)key.GetValue("Provider") }; if (key.GetValueKind("DriverVersion") == RegistryValueKind.Binary) { byte[] versionValue = (byte[])key.GetValue("DriverVersion"); driver.Version = new DriverVersion(ParseRegistryDriverVersion(versionValue)); } else { driver.Version = new DriverVersion((string)key.GetValue("DriverVersion")); } if (key.GetValueKind("DriverDate") == RegistryValueKind.Binary) { byte[] dateValue = (byte[])key.GetValue("DriverDate"); driver.DriverDate = ParseRegistryDriverDate(dateValue); } else { driver.DriverDate = (string)key.GetValue("DriverDate"); } return(driver); }
/// <summary> /// Installs the specified driver. /// </summary> /// <param name="driver">The <see cref="DriverDetails" /> describing the driver to install.</param> public static void Install(DriverDetails driver) { Install(driver, false); }