29

基本的に、私はいくつかのパフォーマンス テストを実行していますが、外部ネットワークをドラッグ要因にしたくありません。ネットワークLANを無効にする方法を検討しています。プログラムでそれを行う効果的な方法は何ですか? C#に興味があります。誰かがポイントを家に持ち帰ることができるコードスニペットを持っていれば、それはクールです.

4

8 に答える 8

35

同じものを検索中にこのスレッドを見つけたので、ここに答えがあります:)

私が C# でテストした最良の方法は、WMI を使用することです。

http://www.codeproject.com/KB/cs/EverythingInWmi02.aspx

msdn の Win32_NetworkAdapter

C# スニペット: (System.Management は、ソリューション内および宣言の使用時に参照する必要があります)

SelectQuery wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL");
ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery);
foreach (ManagementObject item in searchProcedure.Get())
{
    if (((string)item["NetConnectionId"]) == "Local Network Connection")
    {
       item.InvokeMethod("Disable", null);
    }
}
于 2011-05-29T21:22:29.040 に答える
17

netsh コマンドを使用して、「ローカル エリア接続」を有効または無効にできます</p>

  interfaceName is “Local Area Connection”.

  static void Enable(string interfaceName)
    {
     System.Diagnostics.ProcessStartInfo psi =
            new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" enable");
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo = psi;
        p.Start();
    }

    static void Disable(string interfaceName)
    {
        System.Diagnostics.ProcessStartInfo psi =
            new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" disable");
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo = psi;
        p.Start();
    }
于 2013-09-12T10:01:32.040 に答える
1

VB.Net では、トグル ローカル エリア接続にも使用できます。

注: 私自身は Windows XP で使用していますが、ここでは正常に動作します。しかし、Windows 7 では正しく動作しません。

  Private Sub ToggleNetworkConnection()

    Try


        Const ssfCONTROLS = 3


        Dim sConnectionName = "Local Area Connection"

        Dim sEnableVerb = "En&able"
        Dim sDisableVerb = "Disa&ble"

        Dim shellApp = CreateObject("shell.application")
        Dim WshShell = CreateObject("Wscript.Shell")
        Dim oControlPanel = shellApp.Namespace(ssfCONTROLS)

        Dim oNetConnections = Nothing
        For Each folderitem In oControlPanel.items
            If folderitem.name = "Network Connections" Then
                oNetConnections = folderitem.getfolder : Exit For
            End If
        Next


        If oNetConnections Is Nothing Then
            MsgBox("Couldn't find 'Network and Dial-up Connections' folder")
            WshShell.quit()
        End If


        Dim oLanConnection = Nothing
        For Each folderitem In oNetConnections.items
            If LCase(folderitem.name) = LCase(sConnectionName) Then
                oLanConnection = folderitem : Exit For
            End If
        Next


        If oLanConnection Is Nothing Then
            MsgBox("Couldn't find '" & sConnectionName & "' item")
            WshShell.quit()
        End If


        Dim bEnabled = True
        Dim oEnableVerb = Nothing
        Dim oDisableVerb = Nothing
        Dim s = "Verbs: " & vbCrLf
        For Each verb In oLanConnection.verbs
            s = s & vbCrLf & verb.name
            If verb.name = sEnableVerb Then
                oEnableVerb = verb
                bEnabled = False
            End If
            If verb.name = sDisableVerb Then
                oDisableVerb = verb
            End If
        Next



        If bEnabled Then
            oDisableVerb.DoIt()
        Else
            oEnableVerb.DoIt()
        End If


    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
于 2013-09-12T10:54:44.010 に答える
0

ここで他の回答を見ると、うまくいくものもあれば、うまくいかないものもあります。Windows 10 は、このチェーンで以前に使用されたものとは異なる netsh コマンドを使用します。他のソリューションの問題は、ユーザーに表示されるウィンドウを開くことです (ほんの一瞬ですが)。以下のコードは、ネットワーク接続を静かに有効/無効にします。

以下のコードは間違いなくクリーンアップできますが、これは良いスタートです。

*** 動作させるには管理者として実行する必要があることに注意してください ***

//Disable network interface
static public void Disable(string interfaceName)
{
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.FileName = "netsh";
    startInfo.Arguments = $"interface set interface \"{interfaceName}\" disable";
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    System.Diagnostics.Process processTemp = new System.Diagnostics.Process();
    processTemp.StartInfo = startInfo;
    processTemp.EnableRaisingEvents = true;
    try
    {
        processTemp.Start();
    }
    catch (Exception e)
    {
        throw;
    }
}

//Enable network interface
static public void Enable(string interfaceName)
{
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.FileName = "netsh";
    startInfo.Arguments = $"interface set interface \"{interfaceName}\" enable";
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    System.Diagnostics.Process processTemp = new System.Diagnostics.Process();
    processTemp.StartInfo = startInfo;
    processTemp.EnableRaisingEvents = true;
    try
    {
        processTemp.Start();
    }
    catch (Exception e)
    {
        throw;
    }
}
于 2021-07-25T16:44:27.157 に答える