示例#1
0
        private static void ProcessArgs(string[] args)
        {
            CommandProcessor processor = null;

            try
            {
                processor = new CommandProcessor();
            }
            catch (Exception e)
            {
                CommandProcessor.CreateConsole();
                Console.WriteLine("Error: " + e.Message + "\r\n" + "Use -? for help.");
                Console.WriteLine("Press Enter to terminate...");
                Console.ReadLine();
                Environment.Exit(1);
            }

            if (processor.noGui)
            {
                processor.Process();
            }
            else
            {
                try
                {
                    const string SvcPerfExe = "SvcPerf.exe";
                    var          info       = new ProcessStartInfo();
                    string       path       = Path.Combine(SupportFiles.SupportFileDir, "SvcPerfUI.exe");
                    string       exeName    = Assembly.GetExecutingAssembly().Location;
                    string       argsToUi   = string.Empty;

                    try
                    {
                        //By default we pass in a space seperated argument list;
                        argsToUi = args.Length > 0 ? args.Aggregate((curr, next) => curr + " " + next) : string.Empty;
                        int    commandArgStartIndex = Environment.CommandLine.IndexOf("SvcPerf.exe", StringComparison.OrdinalIgnoreCase);
                        string commandLine          = Environment.CommandLine;

                        if (commandArgStartIndex > -1 && commandLine.Length > commandArgStartIndex + SvcPerfExe.Length)
                        {
                            commandLine = commandLine.Substring(commandArgStartIndex + SvcPerfExe.Length);
                            int spaceIndex = commandLine.IndexOf(' ');
                            if (spaceIndex >= 0)
                            {
                                argsToUi = commandLine.Substring(spaceIndex);
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }

                    info.Arguments = argsToUi;
                    info.FileName  = path;

                    if (processor.command == SvcPerfCommand.ShowUIRealtime)
                    {
                        //Realtime sessions require admin rights.
                        info.UseShellExecute = true;
                        info.Verb            = "runas";
                    }

                    Process.Start(info);
                }
                catch (Win32Exception exception)
                {
                    if (exception.NativeErrorCode == 1223)
                    {
                        return;
                    }
                    throw;
                }
            }
        }