public static void DisableAllShares() { INetSharingEveryConnectionCollection connections = SharingManager.EnumEveryConnection; foreach (INetConnection con in connections) { try { INetSharingConfiguration config = GetConfiguration(con); if (config.SharingEnabled) { Console.WriteLine("Sharing was enabled. Disabeling: " + con.ToString()); config.DisableSharing(); } } catch (System.Runtime.InteropServices.ExternalException ex) { Console.WriteLine("DisableAllShares InteropServices.ExternalException: " + ex.Message); } catch (Exception ex) { Console.WriteLine("DisableAllShares EX: " + ex.Message); } } }
public bool Start() { try { INetConnection dialer = GetDialerConnection(); INetConnection wifi = GetAPConnection(); if (dialer == null || wifi == null) { return(false); } INetSharingConfiguration wconf = nsm.INetSharingConfigurationForINetConnection[wifi]; Disable_ICS_WMI(false); if (wconf.SharingEnabled) { wconf.DisableSharing(); } wconf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE); INetSharingConfiguration dconf = nsm.INetSharingConfigurationForINetConnection[dialer]; Disable_ICS_WMI(true); if (dconf.SharingEnabled) { dconf.DisableSharing(); } // System.AccessViolationException??? dconf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC); } catch (Exception e) { Log4Net.WriteLog("Enable sharing wifi failed", e); return(false); } return(true); }
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; } } }