我正在製作一個小程序,讓我的工作更輕鬆。它查找用戶名,主機名,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;
}
}
可以使用哪些代碼來告訴我的功能GetIP(),GetMAC(),GetIP()獲取新數據。如果我一直稱這些功能我所得到的都是相同的舊數據。 – Lgwells1
好吧,我決定試試我的功能計時器。它實際上工作正常,並且對資源沒有壞處。所以我的函數GetIP,GetDNS,GetMAC都更新了他們的數據。現在我遇到問題更新我的工具提示。一旦它熄滅,那就是它,它不會改變。我如何添加一個計時器到我的工具提示Fixes.SetNotifyIconText(ni,showALL());在公共無效顯示()? – Lgwells1