[英]C# Winforms button to specific screen in Windows
我正在編寫一個應用程序來更改網絡適配器IP地址設置。 我將在應用程序中更改只有基本IP設置。
我是否可以使用某種“鏈接”或“路徑”打開通常通過“ TCP / IPv4屬性”屏幕中的“高級”按鈕訪問的“高級TCP / IP設置”屏幕?
上面的屏幕截圖顯示了突出顯示的按鈕,該按鈕通常用於打開高級TCP / IP設置屏幕。 我需要一種直接使用按鈕從應用程序中打開此確切屏幕的方法。
您可以執行以下操作:
System.Diagnostics.Process.Start("ncpa.cpl"); // opens network connections window
Thread.Sleep(500); // give time for window to open
SendKeys.Send("(^a){RIGHT}(%f)r"); // select all (ctrl+a), right arrow, alt+f, r
您可以使用以下代碼來設置網絡適配器的IP地址
public void setIP(string IPAddress,string SubnetMask, string Gateway)
{
ManagementClass objMC = new ManagementClass(
"Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach(ManagementObject objMO in objMOC)
{
if (!(bool) objMO["IPEnabled"])
continue;
try
{
ManagementBaseObject objNewIP = null;
ManagementBaseObject objSetIP = null;
ManagementBaseObject objNewGate = null;
objNewIP = objMO.GetMethodParameters("EnableStatic");
objNewGate = objMO.GetMethodParameters("SetGateways");
//Set DefaultGateway
objNewGate["DefaultIPGateway"] = new string[] {Gateway};
objNewGate["GatewayCostMetric"] = new int[] {1};
//Set IPAddress and Subnet Mask
objNewIP["IPAddress"] = new string[] {IPAddress};
objNewIP["SubnetMask"] = new string[] {SubnetMask};
objSetIP = objMO.InvokeMethod("EnableStatic",objNewIP,null);
objSetIP = objMO.InvokeMethod("SetGateways",objNewGate,null);
Console.WriteLine(
"Updated IPAddress, SubnetMask and Default Gateway!");
}
catch(Exception ex)
{
MessageBox.Show("Unable to Set IP : " + ex.Message); }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.