Example #1
0
        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);
            }
        }
Example #2
0
        public static NetShare GetCurrentlySharedConnections()
        {
            INetConnection sharedConnection = null;
            INetConnection homeConnection   = null;
            INetSharingEveryConnectionCollection connections = SharingManager.EnumEveryConnection;

            foreach (INetConnection c in connections)
            {
                try
                {
                    INetSharingConfiguration config = GetConfiguration(c);
                    if (config.SharingEnabled)
                    {
                        if (config.SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC)
                        {
                            sharedConnection = c;
                            Console.WriteLine("SharedConnection=" + c.ToString());
                        }
                        else if (config.SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE)
                        {
                            homeConnection = c;
                            Console.WriteLine("homeConnection=" + c.ToString());
                        }
                    }
                }
                catch (System.Runtime.InteropServices.ExternalException)
                {
                }
            }

            return(new NetShare(sharedConnection, homeConnection));
        }
Example #3
0
        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);
                }
            }
        }
Example #4
0
 /// <summary>
 /// 禁用所有网络
 /// </summary>
 public static void Disconnect()
 {
     try
     {
         NetSharingManagerClass netSharingMgr             = new NetSharingManagerClass();
         INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection;
         foreach (INetConnection connection in connections)
         {
             try { connection.Disconnect(); } catch { }
         }
     }
     catch { }
 }
Example #5
0
        /*
         *      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);
        }
        /// <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();    //启用网络
                }
            }
        }
Example #7
0
        /// <summary>
        /// 启用所有网络
        /// </summary>
        public static void Connect()
        {
            try
            {
                NetSharingManagerClass netSharingMgr             = new NetSharingManagerClass();
                INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection;
                foreach (INetConnection connection in connections)
                {
                    try { connection.Connect(); } catch { }

                    // // // //INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection);
                    // // // //if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN)
                    // // // //    try { connection.Connect(); } catch { }
                }
            }
            catch { }
        }
Example #8
0
        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(); //禁用网络
                    }
                }
            }
        }
Example #9
0
        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(); //禁用网络
                    }
                }
            }
        }
Example #10
0
        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);
        }