/// <summary> /// Parses the command-line arguments and returns a reference to an /// instance of <see cref="T:adapman.CommandLineInfo" /> whose properties /// are filled with values initialized by the options the user chose. /// </summary> /// <param name="args"> /// Reference to an array of strings that contains the command-line arguments passed to this program. /// </param> /// <returns> /// Reference to an instance of <see cref="T:adapman.CommandLineInfo"/> whose properties /// are filled with the values initialized by the options the user chose; if the user did not /// pass valid command-line arguments, or the command-line could otherwise not be parsed, /// a usage mesage is printed to the screen and this method returns a null reference. /// </returns> public static CommandLineInfo ParseCommandLine(string[] args) { // Skip the first element of args if it contains the application // executable's filename since it's usually initialized by // Environment.GetCommandLineArgs which passes the full path to this // executeable as the first element of args if (args.Length >= 1 && !string.IsNullOrWhiteSpace(args[0]) && args[0].EndsWith("exe")) // strip out the first elt of args, if it ends with "exe" { args = args.Skip(1).ToArray(); } // If we get here, then more arguments exist than simply the path to the executeable itself. // In this case, run the CommandLineValidator to ensure the arguments passed are valid. // (See CommandLineValidator.cs) if (!CommandLineValidator.IsCommandLineValid(args)) { // If the command-line validator determined that the arguments passed are not valid, // print a usage message to the console, and then return the value null instead of // a reference to an instance of CommandLineInfo, so as to signal the caller that the // command-line could not be parsed. PrintUsageMessage(); return(null); } // Getting ready to return the result CommandLineInfo result = null; // Determine what action should be taken based on the value of the first command-line // switch, since the user is required to pass at least one switch to this program. var ssid = string.Empty; var password = string.Empty; switch (args[0].ToLowerInvariant()) // do a case-insensitive comparison { // This switch disables all the network adapters on the user's computer. case "-da": // New up a new CommandLineInfo instance and pass it an action of DisableAllAdapters result = new CommandLineInfo(CommandLineAction.DisableAllAdapters); break; // This switch enables all the network adapters on the user's computer. case "-ea": // New up a new CommandLineInfo instance and pass it an action of EnableAllAdapters result = new CommandLineInfo(CommandLineAction.EnableAllAdapters); break; // The '-cw:ssid' switch, which must be paired with the '-cw:pwd' switch, // connects the user's computer to the Wi-Fi network whose SSID and password // (i.e., network security key) follow the corresponding switches. case "-cw:ssid" when args[2].ToLowerInvariant() == "-cw:pwd":
/// <summary> /// Entry point for the application. /// </summary> /// <param name="args">List of string arguments passed by the user on the command line.</param> public static void Main(string[] args) { // Set up an exception handler for any uncaught exceptions AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; // Check whether the CommandLine property has a value of null. Merely accessing this property should trigger // a call to the CommandLineInfo.ParseCommandLine method, which in turn returns either a reference to an instance // of a CommandLineInfo structure whose properties contain values corresponding to the options specified // by the command-line flags by the user, or, if the command line is empty or invalid, a null reference // is returned. if (CommandLine == null) { // If we are here, print the usage message to remind the user how to specify command line options // to this program, and then exit. CommandLineInfo.PrintUsageMessage(); return; } // The (valid) command-line arguments to this program always specify an action to be taken, which is one of the // values of the CommandLineAction enumeration. Switch on this enumeration's value in order to determine what the // user would like us to do. switch (CommandLine.Action) { // If we arrive here, then the user wants us to disable (i.e., turn off) all the network // adapters on this computer. case CommandLineAction.DisableAllAdapters: // Get a list of all the network adapters on this computer, encapsulated by NetworkAdapter objects // (see NetworkAdapterManager.cs and NetworkAdapter.cs) and then, for each adapter, call its Disable // method to disable it (i.e., turn it off.). foreach (var adapter in NetworkAdapterManager.GetAdapters()) { // Assume the 'adapter' variable holds a non-null reference. NetworkAdapterManager.Disable(adapter); } break; // If we arrive here, then th euser wants us to enable (i.e., turn on) all the network // adapters on this computer. case CommandLineAction.EnableAllAdapters: // Get a list of all the network adapters on this computer, encapsulated by NetworkAdapter objects // (see NetworkAdapterManager.cs and NetworkAdapter.cs) and then, for each adapter, call its Enable // method to enable it (i.e., turn it on). foreach (var adapter in NetworkAdapterManager.GetAdapters()) { // Assume the 'adapter' variable holds a non-null reference. NetworkAdapterManager.Enable(adapter); } break; // If we arrive here, the user wants to connect to a Wi-Fi network with the specified SSID, and, // optionally, with the specified password case CommandLineAction.ConnectWifi: // Call the Connect method of WifiManager to do this operation -- see WifiManager.cs. WifiManager.Connect(CommandLine.WifiSSID, CommandLine.WifiPassword); break; // If we arrive here, the user wants to disconnect from the Wi-Fi network with the specified SSID. case CommandLineAction.DisconnectWifi: // Call the Disconnect method of WifiManager to do this operation -- see WifiManager.cs. Test // to ensure that the user's Wi-Fi adapter is not already in the disconnected state. If that is // so, then there is no need to do a call to the WifiManager.Disconnect method. if (!WifiManager.IsConnected) { // Write a message to the screen informing the user that the Wi-Fi adapter is already // in the disconnected state. Console.WriteLine(Resources.WifiAdapterAlreadyDisconnected); break; } // Disconnect from the Wi-Fi network that has the specified SSID. WifiManager.Disconnect(CommandLine.WifiSSID); break; // This handles the case where the command-line parser somehow validates wrong command-line arguments, and, // as a result, does not pass null to the Main method but triggers this switch/case -- however, if we are here // then this means that the user passed something on the command-line that we do not know what action it corresponds // to. case CommandLineAction.Unknown: break; // If we arrive here, then an invalid value was passed in the CommandLineInfo.Action property. default: throw new ArgumentOutOfRangeException(); } // Done }