public List <NetConnection> GetConnections() { try { netSharingMgr = new NetSharingManagerClass(); connections = netSharingMgr.EnumEveryConnection; var strs = new List <NetConnection>(); foreach (INetConnection connection in connections) { INetSharingConfiguration connSharcf = netSharingMgr.INetSharingConfigurationForINetConnection[connection]; INetConnectionProps connProps = netSharingMgr.NetConnectionProps[connection]; var nc = new NetConnection(connProps.Name, connProps.DeviceName); strs.Add(nc); if (connSharcf.SharingEnabled && connSharcf.SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC) { SharedConnection = nc; } } return(strs); } catch (Exception ex) { MessageBox.Show("网络共享出现不可预知的错误,请手动共享网络。\r\n\r\n错误信息:" + ex.Message, @"虚拟WIFI助手"); return(null); } }
/* * private void DisableSharing() * { * connections = netSharingMgr.EnumEveryConnection; * foreach (INetConnection connection in connections) * { * INetSharingConfiguration connSharcf = netSharingMgr.INetSharingConfigurationForINetConnection[connection]; * INetConnectionProps connProps = netSharingMgr.NetConnectionProps[connection]; * try * { * if (connSharcf.SharingEnabled) * connSharcf.DisableSharing(); * } * catch (Exception e) * { * MessageBox.Show(e.Message); * } * } * } */ public bool EnableSharing(NetConnection nc) { if (nc == null) { return(false); } try { netSharingMgr = new NetSharingManagerClass(); connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { INetSharingConfiguration connSharcf = netSharingMgr.INetSharingConfigurationForINetConnection[connection]; INetConnectionProps connProps = netSharingMgr.NetConnectionProps[connection]; try { //判断要配置的网卡 Microsoft Virtual WiFi Miniport Adapter Realtek PCIe GBE Family Controller if (connProps.DeviceName.Equals(nc.DeviceName)) { //配置WAN连接 if (!connSharcf.SharingEnabled || connSharcf.SharingConnectionType != tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC) { connSharcf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC); } } else if (connProps.DeviceName.Contains("Virtual")) { //配置LAN连接 if (!connSharcf.SharingEnabled || connSharcf.SharingConnectionType != tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC) { connSharcf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE); } } } catch (Exception ex) { MessageBox.Show("网络共享出现不可预知的错误,请手动共享网络。\r\n\r\n错误信息:" + ex.Message, @"虚拟WIFI助手"); return(false); } } SharedConnection = nc; } catch (Exception ex) { MessageBox.Show("网络共享出现不可预知的错误,请手动共享网络。\r\n\r\n错误信息:" + ex.Message, @"虚拟WIFI助手"); return(false); } return(true); }
internal ICSConnection(INetSharingManager nsManager, INetConnection netConnection) { if (nsManager == null) { throw new ArgumentNullException("nsManager"); } if (netConnection == null) { throw new ArgumentNullException("netConnection"); } this._netConnectionProperties = nsManager.NetConnectionProps[netConnection]; this._netSharingConfiguration = nsManager.INetSharingConfigurationForINetConnection[netConnection]; }
/// <summary> /// 重新连接 /// </summary> private void ReConnect() { NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection); if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN) { connection.Disconnect(); //禁用网络 connection.Connect(); //启用网络 } } }
private void AddPrivConn(object sender, EventArgs e) { try { NetSharingManager manager = new NetSharingManagerClass(); INetConnection con = inet_connections[priv_con_cb_index[cb_con_priv.SelectedIndex]]; INetConnectionProps prop = manager.NetConnectionProps[con]; INetSharingConfiguration conf = manager.INetSharingConfigurationForINetConnection[con]; conf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE); priv_con.Add(prop.DeviceName); } catch (Exception) { } UpdateICS(); }
public static void Control(bool isConnect) { NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection); if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN) { if (isConnect) { connection.Connect(); //启用网络 } else { connection.Disconnect(); //禁用网络 } } } }
public void localnetface(string signer) { NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection); if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN) { if (signer == "on") { connection.Connect(); //启用网络 } else if (signer == "off") { connection.Disconnect(); //禁用网络 } } } }
public static INetConnection GetConnectionByName(string name) { INetSharingEveryConnectionCollection connections = GetAllConnections(); foreach (INetConnection c in connections) { try { INetConnectionProps props = GetProperties(c); if (props.Name == name) { return(c); } } catch (System.Runtime.InteropServices.ExternalException) { // Ignore these It'ts known that Tunnel adapter isatap causes getProperties to fail. } } return(null); }
public static void StopSharing() { foreach (INetConnection NetConnection in INetSharingMgr.EnumEveryConnection) { INetConnectionProps INetConProps = INetSharingMgr.NetConnectionProps[NetConnection]; INetSharingConfiguration INetShConfig = INetSharingMgr.INetSharingConfigurationForINetConnection[NetConnection]; if (!INetShConfig.SharingEnabled) { continue; } switch (INetShConfig.SharingConnectionType) { case tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC: INetShConfig.DisableSharing(); Trace.WriteLine( $"\n" + $"Stop ICS Type: Public" + $"\nName: {INetConProps.Name}" + $"\nDeviceName: {INetConProps.DeviceName}" + $"\nGuid: {INetConProps.Guid}" + $"\n"); break; case tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE: INetShConfig.DisableSharing(); Trace.WriteLine( $"\n" + $"Stop ICS Type: Private" + $"\nName: {INetConProps.Name}" + $"\nDeviceName: {INetConProps.DeviceName}" + $"\nGuid: {INetConProps.Guid}" + $"\n"); break; } } }
public static void StartSharing(Guid InterfaceGuid, tagSHARINGCONNECTIONTYPE ShConType) { foreach (INetConnection NetConnection in INetSharingMgr.EnumEveryConnection) { INetConnectionProps INetConProps = INetSharingMgr.NetConnectionProps[NetConnection]; INetSharingConfiguration INetShConfig = INetSharingMgr.INetSharingConfigurationForINetConnection[NetConnection]; if (!Guid.Parse(INetConProps.Guid).Equals(InterfaceGuid)) { continue; } switch (ShConType) { case tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC: INetShConfig.EnableSharing(ShConType); Trace.WriteLine( $"\n" + $"Start ICS Type: Public" + $"\nName: {INetConProps.Name}" + $"\nDeviceName: {INetConProps.DeviceName}" + $"\nGuid: {INetConProps.Guid}" + $"\n"); break; case tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE: INetShConfig.EnableSharing(ShConType); Trace.WriteLine( $"\n" + $"Start ICS Type: Private" + $"\nName: {INetConProps.Name}" + $"\nDeviceName: {INetConProps.DeviceName}" + $"\nGuid: {INetConProps.Guid}" + $"\n"); break; } } }
public static IEnumerable <NetworkInterfaceInfo> GetNetworkInterfaces() { foreach (INetConnection NetConnection in INetSharingMgr.EnumEveryConnection) { NetworkInterfaceInfo NetITFInfo; INetConnectionProps INetConProps = INetSharingMgr.NetConnectionProps[NetConnection]; Guid InterfaceGuid = Guid.Parse(INetConProps.Guid); NetITFInfo = Network.GetNetworkInterfaceInformation(InterfaceGuid); NetITFInfo.InterfaceGuid = InterfaceGuid; NetITFInfo.DeviceName = INetConProps.Name; NetITFInfo.DeviceDescription = INetConProps.DeviceName; NetITFInfo.IsConnectedToInternet = IsNICConnectedToInternet(InterfaceGuid); NetITFInfo.IsConnected = INetConProps.Status == tagNETCON_STATUS.NCS_CONNECTED; yield return(NetITFInfo); switch (INetConProps.MediaType) { case tagNETCON_MEDIATYPE.NCM_NONE: // No media is present. Debug.WriteLine("MediaType: NCM_NONE"); break; case tagNETCON_MEDIATYPE.NCM_DIRECT: // Direct serial connection through a serial port. Debug.WriteLine("MediaType: NCM_DIRECT"); break; case tagNETCON_MEDIATYPE.NCM_ISDN: // Connection is through an integrated services digital network (ISDN) line. Debug.WriteLine("MediaType: NCM_ISDN"); break; case tagNETCON_MEDIATYPE.NCM_LAN: // Connection is to a local area network (LAN). Debug.WriteLine("MediaType: NCM_LAN"); break; case tagNETCON_MEDIATYPE.NCM_PHONE: // Dial-up connection over a conventional phone line. Debug.WriteLine("MediaType: NCM_PHONE"); break; case tagNETCON_MEDIATYPE.NCM_TUNNEL: // Virtual private network (VPN) connection. Debug.WriteLine("MediaType: NCM_TUNNEL"); break; case tagNETCON_MEDIATYPE.NCM_PPPOE: // Point-to-Point protocol (PPP) over Ethernet. Debug.WriteLine("MediaType: NCM_PPPOE"); break; case tagNETCON_MEDIATYPE.NCM_BRIDGE: // Bridged connection. Debug.WriteLine("MediaType: NCM_BRIDGE"); break; case tagNETCON_MEDIATYPE.NCM_SHAREDACCESSHOST_LAN: // Shared connection to a LAN. Debug.WriteLine("MediaType: SHAREDACCESSHOST_LAN"); break; case tagNETCON_MEDIATYPE.NCM_SHAREDACCESSHOST_RAS: // Shared connection to a remote or wide area network (WAN). Debug.WriteLine("MediaType: NCM_SHAREDACCESSHOST_RAS"); break; default: break; } } }
//TODO SLOWWWW void UpdateICS() { NetSharingManager manager = new NetSharingManagerClass(); bool instaled = manager.SharingInstalled; cb_con_pub.Enabled = instaled; lb_con_priv.Enabled = instaled; cb_con_priv.Enabled = instaled; btn_add_privcon.Enabled = instaled; pub_con = new List <string>(); priv_con = new List <string>(); priv_con_cb = new List <string>(); priv_con_cb_index = new List <int>(); int index = 0; int?selected_index = null; foreach (INetConnection con in manager.EnumEveryConnection) { try { inet_connections.Add(con); INetConnectionProps prop = manager.NetConnectionProps[con]; INetSharingConfiguration conf = manager.INetSharingConfigurationForINetConnection[con]; pub_con.Add(prop.Name); if ((tagNETCON_CHARACTERISTIC_FLAGS)prop.Characteristics != tagNETCON_CHARACTERISTIC_FLAGS.NCCF_INCOMING_ONLY && conf.SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC && conf.SharingEnabled) { selected_index = index; } else { if (conf.SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE) { priv_con.Add(prop.Name); } else { priv_con_cb.Add(prop.Name); priv_con_cb_index.Add(index); } } //Info Print(prop.DeviceName + " - " + prop.Status + " - " + prop.MediaType + " - " + conf.SharingEnabled + " - " + conf.SharingConnectionType + " - " + (tagNETCON_CHARACTERISTIC_FLAGS)prop.Characteristics); index++; } catch (Exception e) { } } cb_con_pub.DataSource = pub_con; if (selected_index != null) { cb_con_pub.SelectedIndex = (int)selected_index; } lb_con_priv.DataSource = priv_con; cb_con_priv.DataSource = priv_con_cb; }