/// <summary> /// Enables dynamic DNS resolution. /// </summary> /// <returns></returns> public bool EnableDNS() { //Method closely resembles SetIpAddress, only changes will be highlighted. this.ErrorMsg = string.Empty; String ReturnValue = string.Empty; //Declarations ManagementClass NetConfig = null; ManagementObjectCollection NetCol = null; ManagementBaseObject ErrorCode = null; ManagementBaseObject StaticParams = null; //Set Objects. try { NetConfig = new ManagementClass("Win32_NetworkAdapterConfiguration"); } catch (Exception err) { this.ErrorMsg = string.Format("Error creating network adapter configuration object: {0}", err.Message); return(false); } try { NetCol = NetConfig.GetInstances(); } catch (Exception err) { this.ErrorMsg = string.Format("Error creating network instance collection object: {0}", err.Message); return(false); } //Cycle instances. foreach (ManagementObject NetAdapter in NetCol) { String Caption = (String)NetAdapter["Caption"]; if ((bool)NetAdapter["IPEnabled"] && !Caption.Contains("Microsoft TV/Video Connection")) { //Get Parameters try { //The parameters and methods of SetDNSServerSearchOrder are used in this method. StaticParams = NetAdapter.GetMethodParameters("SetDNSServerSearchOrder"); } catch (Exception err) { this.ErrorMsg = string.Format("Error creating parameters: {0}", err.Message); return(false); } //Set Parameters try { //Set DNSServerSearchOrder to null, causing the network adapter to use DNS. StaticParams["DNSServerSearchOrder"] = null; } catch (Exception err) { this.ErrorMsg = string.Format("Error initializing parameters: {0}", err.Message); return(false); } //Invoke Method. try { //Call SetDNSServerSearchOrder method with null parameters. ErrorCode = NetAdapter.InvokeMethod("SetDNSServerSearchOrder", StaticParams, null); ReturnValue = ErrorCode.GetPropertyValue("ReturnValue").ToString(); } catch (Exception err) { this.ErrorMsg = string.Format("Error enabling DNS: {0}", err.Message); return(false); } } } //Non Exceptional Errors. switch (ReturnValue) { case "0": return(true); case "1": this.ErrorMsg = string.Format("Restart required"); return(true); default: this.ErrorMsg = string.Format("Unexpected error, DNS not enabled: {0}", ReturnValue); return(false); } }
//***Methods***\\ //These methods assume you are working on a computer with a single NIC. /// <summary> /// Sets the IP address of the local machine. Returns false if an error occurs. /// </summary> /// <param name="IPAddr">The new IP Address.</param> /// <param name="SubnetMask">The new subnet mask.</param> /// <param name="Gateway">(Optional)The defualt gateway for the machine. Pass null for defualt.</param> /// <returns></returns> public bool SetIpAddress(String IPAddr, String SubnetMask, String[] Gateway) { //Prepare error handle variables. this.ErrorMsg = string.Empty; String ReturnValue = string.Empty; //Declare management objects as null. ManagementClass NetConfig = null; ManagementObjectCollection NetCol = null; ManagementBaseObject StaticParams = null; ManagementBaseObject GatewayParams = null; ManagementBaseObject ErrorCode = null; //Set Objects try { //Sets netconfig object to all local machine network information. NetConfig = new ManagementClass("Win32_NetworkAdapterConfiguration"); } catch (Exception err) { this.ErrorMsg = string.Format("Error creating network adapter configuration: {0}", err.Message); return(false); } try { //Gets all the specific network instances(this can and will include non network adapters. NetCol = NetConfig.GetInstances(); } catch (Exception err) { this.ErrorMsg = string.Format("Error creating network instance collection: {0}", err.Message); return(false); } //Cycles instances. foreach (ManagementObject NetAdapter in NetCol) { //This string is specifically used to avoid a network instance on Windows Embedded machines, Microsoft TV/Video Connection. String Caption = (String)NetAdapter["Caption"]; //Checks a NetworkAdapterConfiguration property. Property checks if Internet Protocol is enabled on the network instance. if ((bool)NetAdapter["IPEnabled"] && !Caption.Contains("Microsoft TV/Video Connection")) { //If enabled, instance is a network device. Also be careful of the possiblity of "fake" adapters. //Get Parameters. try { //StaticParams holds the parameters for the EnableStatic method. StaticParams = NetAdapter.GetMethodParameters("EnableStatic"); //GatewayParams holds the parameters for the SetGateway method. GatewayParams = NetAdapter.GetMethodParameters("SetGateways"); } catch (Exception err) { this.ErrorMsg = string.Format("Error creating parameters: {0}", err.Message); return(false); } //Set Parameters. try { //IPAddress and SubnetMask are string arrays so the parameters are passed as a new array. StaticParams["IPAddress"] = new String[] { IPAddr }; StaticParams["SubnetMask"] = new String[] { SubnetMask }; //If gateway is null, generate defualt gateway. if (Gateway == null) { //Creates a new string array based on the results of the method call. Gateway = new String[] { DefualtGateway(IPAddr, SubnetMask) }; //If null the gateway creation failed. if (Gateway == null) { this.ErrorMsg = string.Format("Error retrieving defualt gateway with IP {0} and Subnet {1}", IPAddr, SubnetMask); return(false); } } //Set DefaultIPGateway directly to the parameter. GatewayParams["DefaultIPGateway"] = Gateway; } catch (Exception err) { this.ErrorMsg = string.Format("Error initializing parameters: {0}", err.Message); return(false); } //Invoke Methods. try { //Call the NetworkAdapterConfiguration method EnableStatic on the current network instance using the parameter object. ErrorCode = NetAdapter.InvokeMethod("EnableStatic", StaticParams, null); //The return value of the executed command is returned from the Errorcode object into ReturnValue. ReturnValue = ErrorCode.GetPropertyValue("ReturnValue").ToString(); //If no errors are found, set the gateway as well. if (ReturnValue == "0" || ReturnValue == "1") { ErrorCode = NetAdapter.InvokeMethod("SetGateways", GatewayParams, null); ReturnValue = ErrorCode.GetPropertyValue("ReturnValue").ToString(); } } catch (Exception err) { this.ErrorMsg = string.Format("Error invoking methods: {0}", err.Message); return(false); } } } //Non Exceptional Errors. switch (ReturnValue) { case "0": return(true); case "1": this.ErrorMsg = string.Format("Restart required"); return(true); case "66": this.ErrorMsg = string.Format("Invalid subnet mask {0}", SubnetMask); return(false); case "70": this.ErrorMsg = string.Format("Invalid IP Address {0}", IPAddr); return(false); default: this.ErrorMsg = string.Format("Unexpected error, IP address not set: Code {0}", ReturnValue); return(false); } }
/// <summary> /// Enables dynamic IP resolution. /// </summary> /// <returns></returns> public bool EnableDHCP() { //Method closely resembles SetIpAddress, only changes will be highlighted. this.ErrorMsg = string.Empty; String ReturnValue = string.Empty; ManagementClass NetConfig = null; ManagementObjectCollection NetCol = null; ManagementBaseObject ErrorCode = null; //Objects try { NetConfig = new ManagementClass("Win32_NetworkAdapterConfiguration"); } catch (Exception err) { this.ErrorMsg = string.Format("Error creating network adapter configuration object: {0}", err.Message); return(false); } try { NetCol = NetConfig.GetInstances(); } catch (Exception err) { this.ErrorMsg = string.Format("Error creating network instance collection object: {0}", err.Message); return(false); } //Cycles instances. foreach (ManagementObject NetAdapter in NetCol) { String Caption = (String)NetAdapter["Caption"]; if ((bool)NetAdapter["IPEnabled"] && !Caption.Contains("Microsoft TV/Video Connection")) { //Invoke Methods. try { //Invoke EnableDHCP method with no parameters. ErrorCode = NetAdapter.InvokeMethod("EnableDHCP", null, null); ReturnValue = ErrorCode.GetPropertyValue("ReturnValue").ToString(); } catch (Exception err) { this.ErrorMsg = string.Format("Error enabling DHCP: {0}", err.Message); return(false); } } } //Non Exceptional Errors. switch (ReturnValue) { case "0": return(true); case "1": this.ErrorMsg = string.Format("Restart required"); return(true); default: this.ErrorMsg = string.Format("Unexpected error, DHCP not enabled: {0}", ReturnValue); return(false); } }
/// <summary> /// Sets or overwrites the DNS servers for the local machine. /// </summary> /// <param name="DNSservers">An array containing the IP address's of the servers to use.</param> /// <param name="Replace">Set to true if the current DNS servers should be replaced.</param> /// <returns></returns> public bool SetDNSServers(String[] DNSservers, bool Replace) { //Method closely resembles SetIpAddress, only changes will be highlighted. this.ErrorMsg = string.Empty; String ReturnValue = string.Empty; //Declarations ManagementClass NetConfig = null; ManagementObjectCollection NetCol = null; ManagementBaseObject StaticParams = null; ManagementBaseObject ErrorCode = null; //Set Objects. try { NetConfig = new ManagementClass("Win32_NetworkAdapterConfiguration"); } catch (Exception err) { this.ErrorMsg = string.Format("Error creating network adapter configuration object: {0}", err.Message); return(false); } try { NetCol = NetConfig.GetInstances(); } catch (Exception err) { this.ErrorMsg = string.Format("Error creating network instance collection object: {0}", err.Message); return(false); } //Cycle instances. foreach (ManagementObject NetAdapter in NetCol) { String Caption = (String)NetAdapter["Caption"]; if ((bool)NetAdapter["IPEnabled"] && !Caption.Contains("Microsoft TV/Video Connection")) { //Get Parameters. try { //Gets SetDNSServerSearchOrder method parameters. StaticParams = NetAdapter.GetMethodParameters("SetDNSServerSearchOrder"); } catch (Exception err) { this.ErrorMsg = string.Format("Error creating parameters: {0}", err.Message); return(false); } //Set Parameters. try { //Checks if the parameter Replace is true or not. if (Replace) { //If true, the current DNS servers are replaced by the new ones. StaticParams["DNSServerSearchOrder"] = DNSservers; } else { //If false, the new DNS servers are added on to the current ones. //Creates a new array containing the current DNS servers. String[] CrntServers = (String[])NetAdapter["DNSServerSearchOrder"]; //Creates a dynamic array type List to hold both arrays. List <String> list = new List <String>(CrntServers.Length + DNSservers.Length); //Adds the current and new arrays to the list list.AddRange(CrntServers); list.AddRange(DNSservers); //Sets parameter to the list as an array. StaticParams["DNSServerSearchOrder"] = list.ToArray(); } } catch (Exception err) { this.ErrorMsg = string.Format("Error initializing parameters: {0}", err.Message); return(false); } //Invoke Methods. try { //Invoke the SetDNSServerSearchOrder at the new parameters. ErrorCode = NetAdapter.InvokeMethod("SetDNSServerSearchOrder", StaticParams, null); ReturnValue = ErrorCode.GetPropertyValue("ReturnValue").ToString(); } catch (Exception err) { this.ErrorMsg = string.Format("Error invoking method: {0}", err.Message); return(false); } } } //Non Exceptional Errors. switch (ReturnValue) { case "0": return(true); case "1": this.ErrorMsg = string.Format("Restart required"); return(true); case "70": this.ErrorMsg = string.Format("Invalid IP Address:"); for (int i = 0; i < DNSservers.Length; i++) { this.ErrorMsg += string.Format(" {0}", DNSservers[i]); } return(false); default: this.ErrorMsg = string.Format("Unexpected error, DNS not set: Code {0}", ReturnValue); return(false); } }