Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- using System.IO;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Management;
- static class ChangeIP
- {
- static TextBox txtOctet1;
- static ComboBox cboNet;
- static ComboBox cbo1;
- static ComboBox cbo2;
- static ComboBox cbo3;
- static TextBox txtIp;
- static List<Part> sites;
- static void Main()
- {
- string filename = "data.txt";
- string[] args = Environment.GetCommandLineArgs();
- if (args.Length == 2) filename = args[1]; // filnavn medsendt fra bruger
- int x = 100, y = 5;
- int cboWidth = 350;
- Form frm = new Form();
- frm.StartPosition = FormStartPosition.CenterScreen;
- frm.FormBorderStyle = FormBorderStyle.FixedDialog;
- frm.MaximizeBox = false;
- frm.Text = "Skift af IP";
- cboNet = new ComboBox();
- cboNet.DropDownStyle = ComboBoxStyle.DropDownList;
- cboNet.Left = x;
- cboNet.Top = y;
- cboNet.Width = cboWidth;
- txtOctet1 = new TextBox();
- txtOctet1.Width = 50;
- txtOctet1.Left = x;
- txtOctet1.Top = cboNet.Bottom + 2;
- txtOctet1.ReadOnly = true;
- txtOctet1.TabStop = false;
- cbo1 = new ComboBox();
- cbo2 = new ComboBox();
- cbo3 = new ComboBox();
- cbo1.Tag = "cbo1";
- cbo1.SelectedIndexChanged += cbo_SelectedIndexChanged;
- cbo2.SelectedIndexChanged += cbo_SelectedIndexChanged;
- cbo3.SelectedIndexChanged += cbo_SelectedIndexChanged;
- cbo1.DropDownStyle = cbo2.DropDownStyle = cbo3.DropDownStyle = ComboBoxStyle.DropDownList;
- cbo1.Left = cbo2.Left = cbo3.Left = x;
- cbo1.Width = cbo2.Width = cbo3.Width = cboWidth;
- cbo1.Top = txtOctet1.Bottom + 2;
- cbo2.Top = cbo1.Bottom + 2;
- cbo3.Top = cbo2.Bottom + 2;
- txtIp = new TextBox();
- txtIp.Width = 120;
- txtIp.Left = cbo1.Left;
- txtIp.Top = cbo3.Bottom + 2;
- txtIp.ReadOnly = true;
- Label lbl0 = new Label();
- Label lbl1 = new Label();
- Label lbl2 = new Label();
- Label lbl3 = new Label();
- Label lbl4 = new Label();
- lbl0.Top = cboNet.Top;
- lbl1.Top = cbo1.Top;
- lbl2.Top = cbo2.Top;
- lbl3.Top = cbo3.Top;
- lbl4.Top = txtIp.Top;
- lbl0.Text = "Netkort:";
- lbl1.Text = "Landsdel:";
- lbl2.Text = "SiteID:";
- lbl3.Text = "TeknikerNR:";
- lbl4.Text = "IP-adresse:";
- Button cmd = new Button();
- cmd.Click += cmd_Click;
- cmd.Text = "Skift IP";
- cmd.Left = x;
- cmd.Top = txtIp.Bottom + 12;
- cmd.Width = cboWidth;
- cmd.Height = 50;
- frm.Controls.Add(cboNet);
- frm.Controls.Add(txtOctet1);
- frm.Controls.Add(cbo1);
- frm.Controls.Add(cbo2);
- frm.Controls.Add(cbo3);
- frm.Controls.Add(lbl0);
- frm.Controls.Add(lbl1);
- frm.Controls.Add(lbl2);
- frm.Controls.Add(lbl3);
- frm.Controls.Add(lbl4);
- frm.Controls.Add(txtIp);
- frm.Controls.Add(cmd);
- frm.ClientSize = new System.Drawing.Size(cbo1.Right + 5, cmd.Bottom + 5); // bredde,højde
- // net...
- foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
- {
- if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
- {
- cboNet.Items.Add(new NicInfo(nic));
- }
- }
- if (cboNet.Items.Count > 0) cboNet.SelectedIndex = 0; // vælg første netkort som standard
- // hent data fra fil...
- try
- {
- sites = new List<Part>();
- Regex re = new Regex(@"^(\d+)=(\d+)(?:.(?:(\d+).)?(.*))?");
- using (StreamReader sr = new StreamReader(filename))
- {
- while (sr.Peek() >= 0)
- {
- Match m = re.Match(sr.ReadLine());
- if (m.Success)
- {
- switch (m.Groups[1].Value)
- {
- case "0": txtOctet1.Text = m.Groups[2].Value; break;
- case "1": cbo1.Items.Add(new Part(m.Groups[4].Value, m.Groups[2].Value, m.Groups[3].Value)); break;
- case "3": cbo3.Items.Add(new Part(m.Groups[4].Value, m.Groups[2].Value)); break;
- default:
- // ting til den dynamiske liste
- sites.Add(new Part(m.Groups[4].Value, m.Groups[2].Value, m.Groups[1].Value));
- break;
- }
- }
- }
- }
- }
- catch(IOException ex)
- {
- MessageBox.Show("FEJL!\n" + ex.Message);
- return;
- }
- if (frm.ShowDialog() == DialogResult.OK)
- {
- }
- }
- static void cmd_Click(object sender, EventArgs e)
- {
- // skift ip
- if (cboNet.SelectedIndex < 0) return;
- IPAddress ip;
- if (!IPAddress.TryParse(txtIp.Text, out ip))
- {
- MessageBox.Show("Fejl i ip-adresse");
- return;
- }
- try
- {
- var nicInfo = cboNet.SelectedItem as NicInfo;
- NetworkInterface nic = nicInfo.Nic;
- ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- if (mo["SettingID"].ToString() != nic.Id) continue;
- ManagementBaseObject newIp = mo.GetMethodParameters("EnableStatic");
- newIp["IPAddress"] = new string[] { txtIp.Text };
- newIp["SubnetMask"] = new string[] { nicInfo.Mask };
- ManagementBaseObject setIp = mo.InvokeMethod("EnableStatic", newIp, null);
- var ret = (UInt32)setIp["ReturnValue"];
- switch (ret)
- {
- case 0: MessageBox.Show("Din ip-adresse er nu ændret"); break;
- case 1: MessageBox.Show("Ændring træder i kraft efter genstart"); break;
- case 64: MessageBox.Show("Dit styresystem er ikke understøttet"); break;
- case 66: MessageBox.Show("Invalid subnet-maske"); break;
- case 70: MessageBox.Show("Invalid ip"); break;
- case 91: MessageBox.Show("Adgang nægtet"); break;
- case 2147749891: MessageBox.Show("Mangler rettigheder til at ændre ip"); break;
- case 2147786788: MessageBox.Show("Lock-fejl. Se evt. KB950134"); break;
- default: MessageBox.Show("Der opstod en ukendt fejl: " + ret.ToString()); break;
- }
- break;
- }
- }catch(Exception ex)
- {
- MessageBox.Show("Fejl ved ændring af ip-adresse!\r\n" + ex.Message);
- }
- }
- static void cbo_SelectedIndexChanged(object sender, EventArgs e)
- {
- var cbo = sender as ComboBox;
- if (cbo.Tag != null && (string)cbo.Tag == "cbo1")
- {
- // liste 2 skal ændre sig efter liste 1
- cbo2.Items.Clear();
- var part = cbo1.SelectedItem as Part;
- foreach (var subpart in sites.Where(x => x.SubPart == part.SubPart))
- {
- cbo2.Items.Add(subpart);
- }
- }
- if (cbo1.SelectedIndex < 0) return;
- if (cbo2.SelectedIndex < 0) return;
- if (cbo3.SelectedIndex < 0) return;
- string oct2 = (cbo1.Items[cbo1.SelectedIndex] as Part).Value;
- string oct3 = (cbo2.Items[cbo2.SelectedIndex] as Part).Value;
- string oct4 = (cbo3.Items[cbo3.SelectedIndex] as Part).Value;
- txtIp.Text = string.Format("{0}.{1}.{2}.{3}", txtOctet1.Text, oct2, oct3, oct4);
- }
- class NicInfo
- {
- public NetworkInterface Nic { get; set; }
- public string Name;
- public string Mask;
- public NicInfo(NetworkInterface nic)
- {
- this.Nic = nic;
- this.Name = nic.Name;
- var addresses = nic.GetIPProperties().UnicastAddresses;
- foreach (var addr in addresses)
- {
- if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) // ipv4
- {
- string ip = addr.Address.ToString();
- Mask = addr.IPv4Mask.ToString();
- Name += " (" + ip + ")"; // vis ip
- break;
- }
- }
- }
- public override string ToString()
- {
- return Name;
- }
- }
- class Part
- {
- public string Name { get; set; }
- public string Value { get; set; }
- public string SubPart { get; set; }
- public Part(string name, string value, string subPart = null)
- {
- this.Name = name;
- this.Value = value;
- this.SubPart = subPart;
- }
- public override string ToString()
- {
- return Name;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement