private static bool PnpUtilHelper(PnpUtilOptions option, string infName, ref string output)
        {
            bool retVal            = true;
            bool fDebugPrintOutput = false;
            //
            // Setup the process with the ProcessStartInfo class.
            //
            ProcessStartInfo start = new ProcessStartInfo
            {
                FileName               = @"pnputil.exe" /* exe name.*/,
                UseShellExecute        = false,
                RedirectStandardOutput = true,
                CreateNoWindow         = true,
                WindowStyle            = ProcessWindowStyle.Hidden
            };

            switch (option)
            {
            //
            // [jenda_] I also had problems with some arguments starting "-". "/" works fine
            //
            case PnpUtilOptions.Enumerate:
                start.Arguments = @"/e";
                break;

            case PnpUtilOptions.Delete:
                start.Arguments = @"/d " + infName;
                break;

            case PnpUtilOptions.ForceDelete:
                start.Arguments = @"/f /d " + infName;
                break;

            case PnpUtilOptions.Add:
                fDebugPrintOutput      = true;
                start.WorkingDirectory = Path.GetDirectoryName(infName);
                start.Arguments        = @"/a " + Path.GetFileName(infName);
                Trace.TraceInformation($"[Add] workDir = {start.WorkingDirectory}, arguments = {start.Arguments}");
                break;

            case PnpUtilOptions.AddInstall:
                fDebugPrintOutput      = true;
                start.WorkingDirectory = Path.GetDirectoryName(infName);
                start.Arguments        = @"/i /a " + Path.GetFileName(infName);
                Trace.TraceInformation($"[AddInstall] workDir = {start.WorkingDirectory}, arguments = {start.Arguments}");
                break;
            }

            //
            // Start the process.
            //
            try
            {
                using (Process process = Process.Start(start))
                {
                    //
                    // Read in all the text from the process with the StreamReader.
                    //
                    using (StreamReader reader = process.StandardOutput)
                    {
                        string result = reader.ReadToEnd();
                        output = result;
                        if (fDebugPrintOutput)
                        {
                            Trace.TraceInformation($"[Result_start] ---- {Environment.NewLine}{result}[----- Result_End]");
                        }

                        if (option == PnpUtilOptions.Delete || option == PnpUtilOptions.ForceDelete)
                        {
                            // [jenda_] Really don't know, how to recognize error without language-specific string recognition :(
                            // [jenda_] But those errors should contain ":"
                            if (output.Contains(@":"))     //"Deleting the driver package failed"
                            {
                                retVal = false;
                            }
                        }

                        if ((option == PnpUtilOptions.Add || option == PnpUtilOptions.AddInstall))
                        {
                            /* [jenda_]
                             * This regex should recognize (~) this pattern:
                             * MS PnP blah blah
                             *
                             * blah blah blah
                             * blah blah (...)
                             *
                             * blah blah:    *number*
                             * blah blah blah:    *number*
                             *
                             */
                            Match matchResult = AddResultRegex.Match(output);

                            if (matchResult.Success)    // [jenda_] regex recognized successfully
                            {
                                // [jenda_] if trying to add "0" packages or if number packages and number added packages differs
                                if (matchResult.Groups[1].Value == "0" || matchResult.Groups[1].Value != matchResult.Groups[2].Value)
                                {
                                    Trace.TraceError($"Failed to add {infName}");
                                    retVal = false;
                                }
                            }
                            else
                            {
                                Trace.TraceError($"Unknown response while trying to add {infName}");
                                retVal = false;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                // dont catch all exceptions -- but will do for our needs!
                Trace.TraceError($"{e.Message}{Environment.NewLine}{e.StackTrace}");
                output = "";
                retVal = false;
            }

            return(retVal);
        }
Exemple #2
0
        private static bool PnpUtilHelper(PnpUtilOptions option, string infName, ref string output)
        {
            var retVal = true;
            var fDebugPrintOutput = false;
            //
            // Setup the process with the ProcessStartInfo class.
            //
            var start = new ProcessStartInfo
            {
                FileName = @"pnputil.exe" /* exe name.*/,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden
            };
            switch (option)
            {
                //
                // [jenda_] I also had problems with some arguments starting "-". "/" works fine
                //
                case PnpUtilOptions.Enumerate:
                    start.Arguments = @"/e";
                    break;
                case PnpUtilOptions.Delete:
                    start.Arguments = @"/d " + infName;
                    break;
                case PnpUtilOptions.ForceDelete:
                    start.Arguments = @"/f /d " + infName;
                    break;
                case PnpUtilOptions.Add:
                    fDebugPrintOutput = true;
                    start.WorkingDirectory = Path.GetDirectoryName(infName);
                    start.Arguments = @"/a " + Path.GetFileName(infName);
                    Log.DebugFormat("[Add] workDir = {0}, arguments = {1}", start.WorkingDirectory,
                        start.Arguments);
                    break;
                case PnpUtilOptions.AddInstall:
                    fDebugPrintOutput = true;
                    start.WorkingDirectory = Path.GetDirectoryName(infName);
                    start.Arguments = @"/i /a " + Path.GetFileName(infName);
                    Log.DebugFormat("[AddInstall] workDir = {0}, arguments = {1}", start.WorkingDirectory,
                        start.Arguments);

                    break;
            }


            //
            // Start the process.
            //
            var result = "";
            try
            {
                using (var process = Process.Start(start))
                {
                    //
                    // Read in all the text from the process with the StreamReader.
                    //
                    using (var reader = process.StandardOutput)
                    {
                        result = reader.ReadToEnd();
                        output = result;
                        if (fDebugPrintOutput)
                            Log.DebugFormat("[Result_start] ---- {0}{1}[----- Result_End]{0}", Environment.NewLine,
                                result);

                        if (option == PnpUtilOptions.Delete || option == PnpUtilOptions.ForceDelete)
                        {
                            // [jenda_] Really don't know, how to recognize error without language-specific string recognition :(
                            // [jenda_] But those errors should contain ":"
                            if (output.Contains(@":")) //"Deleting the driver package failed"
                            {
                                retVal = false;
                            }
                        }

                        if ((option == PnpUtilOptions.Add || option == PnpUtilOptions.AddInstall))
                        {
                            /* [jenda_]
                             This regex should recognize (~) this pattern:
                             * MS PnP blah blah
                             * 
                             * blah blah blah
                             * blah blah (...)
                             * 
                             * blah blah:    *number*
                             * blah blah blah:    *number*
                             * 
                             */
                            var MatchResult = Regex.Match(output, @".+: +([0-9]+)[\r\n].+: +([0-9]+)[\r\n ]+",
                                RegexOptions.Singleline);

                            if (MatchResult.Success) // [jenda_] regex recognized successfully
                            {
                                // [jenda_] if trying to add "0" packages or if number packages and number added packages differs
                                if (MatchResult.Groups[1].Value == "0" ||
                                    MatchResult.Groups[1].Value != MatchResult.Groups[2].Value)
                                {
                                    Log.ErrorFormat("[Error] failed to add " + infName);
                                    retVal = false;
                                }
                            }
                            else
                            {
                                Log.ErrorFormat("[Error] unknown response while trying to add " + infName);
                                retVal = false;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                // dont catch all exceptions -- but will do for our needs!
                Log.ErrorFormat(@"{0}\n{1}" + Environment.NewLine, e.Message, e.StackTrace);
                output = "";
                retVal = false;
            }
            return retVal;
        }