2013-07-23 47 views
0

我正在制作一个小程序,让我的工作更轻松。它查找用户名,主机名,MAC地址和IP地址。C#当新数据存在时,如何更新或刷新函数?

当用户将鼠标悬停在系统托盘图标上时,会出现一个工具提示,其中包含所有信息。截至目前,它的效果很好,除非有新的网络连接出现,否则它不会获得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; 
     } 
    } 

回答

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"); 
     } 
+0

可以使用哪些代码来告诉我的功能GetIP(),GetMAC(),GetIP()获取新数据。如果我一直称这些功能我所得到的都是相同的旧数据。 – Lgwells1

+0

好吧,我决定试试我的功能计时器。它实际上工作正常,并且对资源没有坏处。所以我的函数GetIP,GetDNS,GetMAC都更新了他们的数据。现在我遇到问题更新我的工具提示。一旦它熄灭,那就是它,它不会改变。我如何添加一个计时器到我的工具提示Fixes.SetNotifyIconText(ni,showALL());在公共无效显示()? – Lgwells1

0

只要数据发生变化,您就可以广播一个信号,并向侦听该广播的侦听器注册您的功能。 registerReceiver可以用于以后的广播信号,它们应该是C#中的一些函数,比如Android中的Intent。

相关问题