ParseArgs() public method

public ParseArgs ( string args ) : void
args string
return void
コード例 #1
0
        /// <summary>
        /// Parses the NAPS2 GUI command-line arguments.
        /// </summary>
        /// <param name="args"></param>
        public void ParseArgs(string[] args)
        {
            bool silent      = args.Any(x => x.Equals("/Silent", StringComparison.InvariantCultureIgnoreCase));
            bool noElevation = args.Any(x => x.Equals("/NoElevation", StringComparison.InvariantCultureIgnoreCase));

            // Utility function to send a message to the user (if /Silent is not specified)
            void Out(string message)
            {
                if (!silent)
                {
                    MessageBox.Show(message);
                }
            }

            // Utility function to run the given action, elevating to admin permissions if necessary (and /NoElevation is not specified)
            bool ElevationRequired(Action action)
            {
                try
                {
                    action();
                    return(true);
                }
                catch (Exception)
                {
                    if (!noElevation && !IsElevated)
                    {
                        RelaunchAsElevated();
                        return(false);
                    }
                    throw;
                }
            }

            // Let StillImage figure out what it should do from the command-line args
            sti.ParseArgs(args);

            // Actually do any specified StillImage actions
            if (sti.ShouldRegister)
            {
                try
                {
                    if (ElevationRequired(sti.Register))
                    {
                        Out("Successfully registered STI. A reboot may be needed.");
                    }
                }
                catch (Exception ex)
                {
                    Log.ErrorException("Error registering STI", ex);
                    Out("Error registering STI. Maybe run as administrator?");
                    returnCode = 1;
                }
            }
            else if (sti.ShouldUnregister)
            {
                try
                {
                    if (ElevationRequired(sti.Unregister))
                    {
                        Out("Successfully unregistered STI. A reboot may be needed.");
                    }
                }
                catch (Exception ex)
                {
                    Log.ErrorException("Error unregistering STI", ex);
                    Out("Error unregistering STI. Maybe run as administrator?");
                    returnCode = 1;
                }
            }

            shouldCreateEventSource = args.Any(x => x.Equals("/CreateEventSource", StringComparison.InvariantCultureIgnoreCase));
            if (shouldCreateEventSource)
            {
                try
                {
                    if (ElevationRequired(windowsEventLogger.CreateEventSource))
                    {
                        Out("Successfully created event source.");
                    }
                }
                catch (Exception ex)
                {
                    Log.ErrorException("Error creating event source", ex);
                    Out("Error creating event source. Maybe run as administrator?");
                    returnCode = 1;
                }
            }
        }
コード例 #2
0
ファイル: Lifecycle.cs プロジェクト: ziceptor/naps2
 /// <summary>
 /// Parses the NAPS2 GUI command-line arguments.
 /// </summary>
 /// <param name="args"></param>
 public void ParseArgs(string[] args)
 {
     sti.ParseArgs(args);
 }