/// <summary> /// Set's a new IP Address and it's Submask of the local machine /// </summary> /// <param name="ip_address">The IP Address</param> /// <param name="subnet_mask">The Submask IP Address</param> /// <remarks>Requires a reference to the System.Management namespace</remarks> public void SetIP(CraigsListEmailAccount account) { using (var networkConfigMng = new ManagementClass("Win32_NetworkAdapterConfiguration")) { using (var networkConfigs = networkConfigMng.GetInstances()) { foreach (var managementObject in networkConfigs.Cast <ManagementObject>().Where(managementObject => (bool)managementObject["IPEnabled"])) { using (var newIP = managementObject.GetMethodParameters("EnableStatic")) { // Set new IP address and subnet if needed if (!String.IsNullOrEmpty(account.Proxy)) { newIP["IPAddress"] = new[] { account.Proxy }; newIP["SubnetMask"] = new[] { "255.255.255.128" }; } managementObject.InvokeMethod("EnableStatic", newIP, null); // Set mew gateway if needed using (var newGateway = managementObject.GetMethodParameters("SetGateways")) { if (account.Proxy.Contains("12.31.180")) { newGateway["DefaultIPGateway"] = new[] { "12.31.180.129" } } ; if (account.Proxy.Contains("12.35.215")) { newGateway["DefaultIPGateway"] = new[] { "12.35.215.129" } } ; newGateway["GatewayCostMetric"] = new[] { 1 }; managementObject.InvokeMethod("SetGateways", newGateway, null); } } } } } }
public static List <CraigsListEmailAccount> PopulateIps() { var context = new whitmanenterprisecraigslistEntities() { CommandTimeout = 18000 }; var result = new List <CraigsListEmailAccount>(); int position = 1; var emailListPerPcId = context.vinclappemailaccounts.Where(x => x.Active == true && !String.IsNullOrEmpty(x.ProxyIP) && x.DefaultGateway.Equals("12.31.180.129")); foreach (var tmp in emailListPerPcId) { if (!String.IsNullOrEmpty(tmp.AccountName) && !String.IsNullOrEmpty(tmp.AccountPassword)) { var clAccount = new CraigsListEmailAccount() { CraigslistAccount = tmp.AccountName, CraigsListPassword = tmp.AccountPassword, CraigsAccountPhoneNumber = tmp.Phone, Proxy = tmp.ProxyIP, isCurrentlyUsed = false, Position = position++, DefaultGateWay = tmp.DefaultGateway, Dns = tmp.Dns }; result.Add(clAccount); } } if (result.Any()) { result.First().isCurrentlyUsed = true; } return(result); }