我需要檢查我的 mac 地址是否存在于包含許多 mac 地址的檔案中?
public static string ismac;
public static bool resultrr;
string path = Path.GetTempPath() "555.txt";
WebClient clienst = new WebClient();
clienst.DownloadFile(@"http://localhost/test/mac.txt",path);
string[] ssv = File.ReadAllLines(path);
foreach (string items in ssv)
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
ismac= nic.GetPhysicalAddress().ToString();
if (items.Contains(ismac) == false)
{
resultrr = false;
}
else
{
resultrr = true;
}
}
Console.ReadKey();
}
我感到困惑,任何幫助獲得作業界面mac并將其與文本檔案進行比較?
uj5u.com熱心網友回復:
我的 Visual Studio 仍在更新。我在記事本上寫這個,所以請原諒錯別字。
因此,只需對現有代碼進行最少的更改,這應該可以作業 -
public static string ismac;
public static bool resultrr;
string path = Path.GetTempPath() "555.txt";
WebClient clienst = new WebClient();
clienst.DownloadFile(@"http://localhost/test/mac.txt",path);
string[] ssv = File.ReadAllLines(path);
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
ismac = nic.GetPhysicalAddress().ToString();
resultrr = ssv.Any(x => x.Contains(ismac));
if(resultrr) break;
}
Console.ReadKey();
resultrr如果您的 mac 在串列中,這應該設定為 true。
您應該在您的代碼中重新考慮更多的事情,例如在您的客戶端周圍使用陳述句替換WebClient為HttpClient, using 。using
uj5u.com熱心網友回復:
在您的代碼中,如果 MAC 地址匹配,它會將 resultrr 設定為 true,然后在下一次迭代中立即將其再次設定為 false。
如果您希望它在單個匹配中回傳 true,則可以將其拆分為自己的方法,并且僅return true在第一個匹配上。
public bool MACFound()
{
…
string[] ssv = File.ReadAllLines(path);
foreach (string items in ssv)
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
ismac= nic.GetPhysicalAddress().ToString();
if (items.Contains(ismac) == true)
{
return true;
}
}
}
return false;
}
如果不是,您至少需要在回圈開始之前將 returnrr 設定為 false 并在回圈中洗掉對 false 的賦值。不過,除非您正在做其他事情,例如計算出現次數,否則沒有理由不盡早跳出回圈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/449500.html
標籤:C# 前锋 视觉工作室 2015 控制台应用程序 网络客户端
