0

仕事での生活を楽にする小さなプログラムに取り組んでいます。ユーザー名、ホスト名、MAC アドレス、および IP アドレスを探します。

ユーザーがマウスをシステム トレイ アイコンの上に置くと、ツール ヒントにすべての情報が表示されます。現時点では、新しいネットワーク接続が提示されたときにIPを取得しないことを除いて、うまく機能します.

新しい IP アドレスを取得する前に、プログラムをシャットダウンして再度開く必要があります。アプリケーションを再起動せずに、新しく検出されたネットワーク接続の IP アドレスを取得するにはどうすればよいですか。

時間指定の更新は必要ないと思います。リソースを大量に消費する可能性があります。アプリケーションの肉とジャガイモを含めました。

class ProcessIcon : IDisposable
{
    /// <summary>
    /// The NotifyIcon object.
    /// </summary>
    NotifyIcon ni;

    /// <summary>
    /// Initializes a new instance of the <see cref="ProcessIcon"/> class.
    /// </summary>
    // Instantiate the NotifyIcon object.
    public ProcessIcon()
    {
        ni = new NotifyIcon();
    }

    //Get DNS of computer
    public static string GetDNS()
    {
        String strHostName = string.Empty;
        strHostName = Dns.GetHostName();
        return strHostName;
    }

    //Get IP Address(s) of computer
    static public string GetIP()
    {
        string strReturn = string.Empty;

        //This gets the computers DNS
        String strHostName = string.Empty;
        strHostName = Dns.GetHostName();

        // Then using host name, get the IP address list..
        IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
        IPAddress[] addr = ipEntry.AddressList;

        List<string> lstIP = new List<string>();

        for (int i = 0; i < addr.Length; i++)
        {
            if (addr[i].AddressFamily == AddressFamily.InterNetworkV6)
            {
                    //DO NOTHING. This If statements checks for IPV6 addresses and excludes them from the output.
            }
            else
            {
                //This if statement checks if the address is a IPV4 and if it is, it adds it to the string.
                if (addr[i].AddressFamily == AddressFamily.InterNetwork)
                {
                    strReturn += (addr[i].ToString() + "\t");
                }
                else
                {
                        //Nothing for now
                }
            }                
        }     
        return strReturn;
    }

    //Gets the computers MAC address for ethernet
    public static string getMAC()
    {
        string macAddresses = "";
        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
        // Only consider Ethernet network interfaces, thereby ignoring any
        // loopback devices etc.
        if (nic.NetworkInterfaceType != NetworkInterfaceType.Ethernet) continue;
            if (nic.OperationalStatus == OperationalStatus.Up)
            {
                macAddresses += nic.GetPhysicalAddress().ToString();
                break;
            }
        }
        return macAddresses;    
    }

    public static string GetUSER()
    {
        string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

        return username;
    }


    //String that combines the DNS,MAC and IP address strings into one that is formatted for easy viewing.
    public static string showALL()
    {
        string showALL = "User: " + GetUSER() + Environment.NewLine + "DNS: " + GetDNS() + Environment.NewLine + "MAC: " + getMAC() + Environment.NewLine + "IP: " + GetIP();
        return showALL;
    }

    /// <summary>
    /// Displays the icon in the system tray.
    /// </summary>
    public void Display()
    {
        // Put the icon in the system tray and allow it react to mouse clicks.          
        ni.MouseClick += new MouseEventHandler(ni_MouseClick1);
        ni.Icon = Resources.SystemTrayApp;
        Fixes.SetNotifyIconText(ni,showALL());
        ni.Visible = true;

        // Attach a context menu.
        ni.ContextMenuStrip = new ContextMenus().Create();
    }

    private void ni_MouseClick(object sender, MouseEventArgs e)
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Releases unmanaged and - optionally - managed resources
    /// </summary>

    // When the application closes, this will remove the icon from the system tray immediately.
    public void Dispose()
    {
        ni.Dispose();
    }

    /// <summary>
    /// Handles the MouseClick event of the ni control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
    void ni_MouseClick1 (object sender, MouseEventArgs e)
    {
        // Handle mouse button clicks.
        if (e.Button == MouseButtons.Left)
        {

        Form1 form1 = new Form1();

        form1.Text = "Whats my IP";
        form1.Show();
        form1.ShowInTaskbar = false;
        }
    }
4

2 に答える 2

0

イベント NetworkChange.NetworkAddressChanged を追加し、アクションを実行してデータを更新できます。例:

        static void Main(string[] args) 
        { 
              try 
              { 
                    NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged); 
                    Console.ReadLine(); 
              } 
              catch (Exception e) 
              { 
                    Console.WriteLine(e); 
              } 
        } 

        static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e) 
        { 
              Console.WriteLine(" do Something"); 
        }
于 2013-07-23T04:54:31.477 に答える