Определить шлюз по умолчанию устройств в локальной сети - C#
Формулировка задачи:
Всем доброго времени суток. Имеем большую локальную сеть с несколькими подсетями, в каждой из которых порядка 10-20 компов. вся сеть в домене. Знает ли кто-нибудь как узнать шлюз по умолчанию у каждого компьютера в сети?
Решение задачи: «Определить шлюз по умолчанию устройств в локальной сети»
textual
Листинг программы
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Management;
- namespace WMISample
- {
- public class MyQuerySample : System.Windows.Forms.Form
- {
- private System.Windows.Forms.Label userNameLabel;
- private System.Windows.Forms.TextBox userNameBox;
- private System.Windows.Forms.TextBox passwordBox;
- private System.Windows.Forms.Label passwordLabel;
- private System.Windows.Forms.Button OKButton;
- private System.Windows.Forms.Button cancelButton;
- private System.ComponentModel.Container components = null;
- public MyQuerySample()
- {
- InitializeComponent();
- }
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- private void InitializeComponent()
- {
- this.userNameLabel = new System.Windows.Forms.Label();
- this.userNameBox = new System.Windows.Forms.TextBox();
- this.passwordBox = new System.Windows.Forms.TextBox();
- this.passwordLabel = new System.Windows.Forms.Label();
- this.OKButton = new System.Windows.Forms.Button();
- this.cancelButton = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // userNameLabel
- //
- this.userNameLabel.Location = new System.Drawing.Point(16, 8);
- this.userNameLabel.Name = "userNameLabel";
- this.userNameLabel.Size = new System.Drawing.Size(160, 32);
- this.userNameLabel.TabIndex = 0;
- this.userNameLabel.Text = "Enter the user name for the remote computer:";
- //
- // userNameBox
- //
- this.userNameBox.Location = new System.Drawing.Point(160, 16);
- this.userNameBox.Name = "userNameBox";
- this.userNameBox.Size = new System.Drawing.Size(192, 20);
- this.userNameBox.TabIndex = 1;
- this.userNameBox.Text = "";
- //
- // passwordBox
- //
- this.passwordBox.Location = new System.Drawing.Point(160, 48);
- this.passwordBox.Name = "passwordBox";
- this.passwordBox.PasswordChar = '*';
- this.passwordBox.Size = new System.Drawing.Size(192, 20);
- this.passwordBox.TabIndex = 3;
- this.passwordBox.Text = "";
- //
- // passwordLabel
- //
- this.passwordLabel.Location = new System.Drawing.Point(16, 48);
- this.passwordLabel.Name = "passwordLabel";
- this.passwordLabel.Size = new System.Drawing.Size(160, 32);
- this.passwordLabel.TabIndex = 2;
- this.passwordLabel.Text = "Enter the password for the remote computer:";
- //
- // OKButton
- //
- this.OKButton.Location = new System.Drawing.Point(40, 88);
- this.OKButton.Name = "OKButton";
- this.OKButton.Size = new System.Drawing.Size(128, 23);
- this.OKButton.TabIndex = 4;
- this.OKButton.Text = "OK";
- this.OKButton.Click += new System.EventHandler(this.OKButton_Click);
- //
- // cancelButton
- //
- this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
- this.cancelButton.Location = new System.Drawing.Point(200, 88);
- this.cancelButton.Name = "cancelButton";
- this.cancelButton.Size = new System.Drawing.Size(128, 23);
- this.cancelButton.TabIndex = 5;
- this.cancelButton.Text = "Cancel";
- this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
- //
- // MyQuerySample
- //
- this.AcceptButton = this.OKButton;
- this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
- this.CancelButton = this.cancelButton;
- this.ClientSize = new System.Drawing.Size(368, 130);
- this.ControlBox = false;
- this.Controls.Add(this.cancelButton);
- this.Controls.Add(this.OKButton);
- this.Controls.Add(this.passwordBox);
- this.Controls.Add(this.passwordLabel);
- this.Controls.Add(this.userNameBox);
- this.Controls.Add(this.userNameLabel);
- this.Name = "MyQuerySample";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "Remote Connection";
- this.ResumeLayout(false);
- }
- [STAThread]
- static void Main()
- {
- Application.Run(new MyQuerySample());
- }
- private void OKButton_Click(object sender, System.EventArgs e)
- {
- try
- {
- ConnectionOptions connection = new ConnectionOptions();
- connection.Username = userNameBox.Text;
- connection.Password = passwordBox.Text;
- connection.Authority = "ntlmdomain:MY_DOMAIN";
- ManagementScope scope = new ManagementScope(
- "\\\\FullComputerName\\root\\CIMV2", connection);
- scope.Connect();
- ObjectQuery query= new ObjectQuery(
- "SELECT * FROM Win32_NetworkAdapterConfiguration");
- ManagementObjectSearcher searcher =
- new ManagementObjectSearcher(scope, query);
- foreach (ManagementObject queryObj in searcher.Get())
- {
- Console.WriteLine("-----------------------------------");
- Console.WriteLine("Win32_NetworkAdapterConfiguration instance");
- Console.WriteLine("-----------------------------------");
- if(queryObj["DefaultIPGateway"] == null)
- Console.WriteLine("DefaultIPGateway: {0}", queryObj["DefaultIPGateway"]);
- else
- {
- String[] arrDefaultIPGateway = (String[])(queryObj["DefaultIPGateway"]);
- foreach (String arrValue in arrDefaultIPGateway)
- {
- Console.WriteLine("DefaultIPGateway: {0}", arrValue);
- }
- }
- }
- Close();
- }
- catch(ManagementException err)
- {
- MessageBox.Show("An error occurred while querying for WMI data: " + err.Message);
- }
- catch(System.UnauthorizedAccessException unauthorizedErr)
- {
- MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);
- }
- }
- private void cancelButton_Click(object sender, System.EventArgs e)
- {
- Close();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д