答案1
在早期的操作系统版本上netsh routing
可以使用,但在 Win7 中不再起作用。
- 看一下这个 Powershell 代码片段,否则 StackOverflow 可能有一个 C# 示例:
# Register the HNetCfg library (once) RegSvr32 "hnetcfg.dll" # Create a NetSharingManager object $m = New-Object -ComObject HNetCfg.HNetShare # List connections $m.EnumEveryConnection |% { $m.NetConnectionProps.Invoke($_) } # Find connection $c = $m.EnumEveryConnection |? { $m.NetConnectionProps.Invoke($_).Name -eq "Ethernet" } # Get sharing configuration $config = $m.INetSharingConfigurationForINetConnection.Invoke($c) # See if sharing is enabled Write-Output $config.SharingEnabled # See the role of connection in sharing, only meaningful if SharingEnabled is True # 0: Public || 1: Private Write-Output $config.SharingType # Enable sharing # 0: Public || 1: Private $config.EnableSharing(0) # Disable sharing $config.DisableSharing()
- 如何使用命令行启用 Internet 连接共享?