Beispiel #1
0
 public bool EnableSharing(string connectionToShare, string sharedForConnection)
 {
     try
     {
         var manager     = new NetSharingManager();
         var connections = manager.EnumEveryConnection;
         foreach (INetConnection c in connections)
         {
             var props      = manager.NetConnectionProps[c];
             var sharingCfg = manager.INetSharingConfigurationForINetConnection[c];
             if (props.Name == connectionToShare)
             {
                 //MessageBox.Show(connectionToShare.ToString());
                 sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
             }
             else if (props.Name == sharedForConnection)
             {
                 //MessageBox.Show(sharedForConnection.ToString());
                 sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
             }
         }
         return(true);
     }
     catch
     {
         return(false);
     }
 }
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                string connectionToShare = this.textBox3.Text; // 被共享的网络连接
                string sharedForConnection = this.textBox4.Text; // 需要共享的网络连接

                NetSharingManager manager = new NetSharingManager();
                var connections = manager.EnumEveryConnection;

                foreach (INetConnection c in connections)
                {
                    var props = manager.NetConnectionProps[c];
                    var sharingCfg = manager.INetSharingConfigurationForINetConnection[c];
                    if (props.Name == connectionToShare)
                    {
                        sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
                    }
                    else if (props.Name == sharedForConnection)
                    {
                        sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
                    }
                }
            }
            catch
            {
                MessageBox.Show("请打开网络和共享中心·查看是不是已经连接Internet!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Beispiel #3
0
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                string connectionToShare   = this.textBox3.Text; // 被共享的网络连接
                string sharedForConnection = this.textBox4.Text; // 需要共享的网络连接

                NetSharingManager manager = new NetSharingManager();
                var connections           = manager.EnumEveryConnection;

                foreach (INetConnection c in connections)
                {
                    var props      = manager.NetConnectionProps[c];
                    var sharingCfg = manager.INetSharingConfigurationForINetConnection[c];
                    if (props.Name == connectionToShare)
                    {
                        sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
                    }
                    else if (props.Name == sharedForConnection)
                    {
                        sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
                    }
                }
            }
            catch
            {
                MessageBox.Show("请打开网络和共享中心·查看是不是已经连接Internet!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Beispiel #4
0
        public static void EnableInternetConnectionSharing(string Share)
        {
            try
            {
                string connectionToShare   = Share;
                string sharedForConnection = "无线网络连接 2";

                var manager     = new NetSharingManager();
                var connections = manager.EnumEveryConnection;

                foreach (INetConnection c in connections)
                {
                    var props      = manager.NetConnectionProps[c];
                    var sharingCfg = manager.INetSharingConfigurationForINetConnection[c];
                    if (props.Name == connectionToShare)
                    {
                        sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
                    }
                    else if (props.Name == sharedForConnection)
                    {
                        sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
                    }
                }
            }
            catch (Exception c)
            {
                MessageBox.Show(c.Message);
            }
        }
Beispiel #5
0
        private void DisableSharing()
        {
            if (router.IsRunning())
            {
                INetSharingManager sharingManager = new NetSharingManager();
                foreach (var con in from INetSharingConfiguration conf in from INetConnection c in sharingManager.EnumEveryConnection
                         select sharingManager.INetSharingConfigurationForINetConnection[c]
                         where conf.SharingEnabled
                         select conf)
                {
                    con.DisableSharing();
                }

                var scope = new ManagementScope("root\\Microsoft\\HomeNet");
                scope.Connect();

                foreach (var type in new string[] { "HNet_ConnectionProperties", "HNet_Connection" })
                {
                    var query = new ObjectQuery("SELECT * FROM " + type);
                    var srchr = new ManagementObjectSearcher(scope, query);
                    foreach (ManagementObject entry in srchr.Get())
                    {
                        entry.Dispose();
                        entry.Delete();
                    }
                }
                {
                    var options = new PutOptions();
                    options.Type = PutType.UpdateOnly;

                    var query = new ObjectQuery("SELECT * FROM HNet_ConnectionProperties");
                    var srchr = new ManagementObjectSearcher(scope, query);
                    foreach (ManagementObject entry in srchr.Get())
                    {
                        if ((bool)entry["IsIcsPrivate"])
                        {
                            entry["IsIcsPrivate"] = false;
                        }
                        if ((bool)entry["IsIcsPublic"])
                        {
                            entry["IsIcsPublic"] = false;
                        }
                        entry.Put(options);
                    }
                }
            }
        }
Beispiel #6
0
 private void refresh()
 {
     try
     {
         internet_sharing_box.Items.Clear();
         internet_sharing_box.Items.Add(new ComboBoxItem {
             Content = NO_INTERNET_SHARING
         });
         internet_sharing_box.SelectedIndex = 0;
         INetSharingManager sharingManager = new NetSharingManager();
         foreach (var con in from INetConnection c in sharingManager.EnumEveryConnection
                  where sharingManager.NetConnectionProps[c].Status == tagNETCON_STATUS.NCS_CONNECTED
                  select c)
         {
             if (sharingManager.NetConnectionProps[con].DeviceName.StartsWith("Microsoft "))
             {
                 string con_name = sharingManager.NetConnectionProps[con].Name + System.Environment.NewLine + sharingManager.NetConnectionProps[con].DeviceName;
                 internet_sharing_box.Items.Add(new ComboBoxItem {
                     Content = con_name
                 });
                 control_btn_change(1, true);
             }
             else
             {
                 string con_name = sharingManager.NetConnectionProps[con].Name + System.Environment.NewLine + sharingManager.NetConnectionProps[con].DeviceName;
                 internet_sharing_box.Items.Add(new ComboBoxItem {
                     Content = con_name
                 });
                 if (sharingManager.INetSharingConfigurationForINetConnection[con].SharingEnabled && sharingManager.INetSharingConfigurationForINetConnection[con].SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC)
                 {
                     internet_sharing_box.SelectedValue = con_name;
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Exception", MessageBoxButton.OK);
     }
 }
Beispiel #7
0
        private async void ctrl_button_click()
        {
            control_btn_change(0, true);
            var ssid  = ssid_box.Text;
            var key   = password_box.Password;
            var share = internet_sharing_box.SelectedIndex != 0 ? internet_sharing_box.SelectedValue.ToString().Split(Environment.NewLine.ToArray(), StringSplitOptions.None).Last() : null;

            sharedaccess.SetValue("ScopeAddress", RouterIP.Text);
            await background_dispatcher.InvokeAsync(async() =>
            {
                switch (ctrl_key_state)
                {
                case true:
                    try
                    {
                        router.SSID = ssid;
                        router.Key  = key;
                        router.Start();
                        DisableSharing();
                        INetSharingManager sharingManager = new NetSharingManager();
                        INetConnection Router             = (from INetConnection con in sharingManager.EnumEveryConnection
                                                             where sharingManager.NetConnectionProps[con].Status == tagNETCON_STATUS.NCS_CONNECTED
                                                             where sharingManager.NetConnectionProps[con].DeviceName.StartsWith("Microsoft ")
                                                             select con).First();
                        if (share != null)
                        {
                            INetConnection Freigabe = (from INetConnection con in sharingManager.EnumEveryConnection where sharingManager.get_NetConnectionProps(con).DeviceName == share select con).First();
                            sharingManager.INetSharingConfigurationForINetConnection[Freigabe].EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
                            sharingManager.INetSharingConfigurationForINetConnection[Router].EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
                        }
                    }
                    catch (Exception ex)
                    {
                        await Dispatcher.InvokeAsync(() => {
                            MessageBox.Show(ex.Message, FAILED, MessageBoxButton.OK);
                            control_btn_change(2, true);
                        }, DispatcherPriority.Normal);
                    }
                    await Dispatcher.InvokeAsync(refresh, DispatcherPriority.Normal);
                    break;

                case false:
                    try
                    {
                        DisableSharing();
                        router.Stop();
                        await Dispatcher.InvokeAsync(() => {
                            refresh();
                            control_btn_change(2, true);
                        }, DispatcherPriority.Normal);
                    }
                    catch (Exception ex)
                    {
                        await Dispatcher.InvokeAsync(() => {
                            MessageBox.Show(FAILDED_TO_STOP + System.Environment.NewLine + ex.Message, "Error", MessageBoxButton.OK);
                            control_btn_change(1, true);
                        }, DispatcherPriority.Normal);
                    }
                    break;
                }
            }, DispatcherPriority.Normal);
        }
        public bool JShareWIFI(bool isShare, out string jShareWIFIRet)
        {
            jShareWIFIRet = "未找到本地网络连接!";
            if (!isShare)
            {
                if (sharingCfg != null)
                {
                    sharingCfg = null;
                }
                return(true);
            }
            try
            {
                string connectionToShare   = getBestInterface()?.Name; // 被共享的网络连接
                string sharedForConnection = "";                       // 共享的家庭网络连接

                var manager     = new NetSharingManager();
                var connections = manager.EnumEveryConnection;

                NetworkInterface[] Ninterface = NetworkInterface.GetAllNetworkInterfaces();//确定虚拟网络名称
                foreach (NetworkInterface IN in Ninterface)
                {
                    if (IN.Description.Contains("Microsoft Hosted Network Virtual Adapter") && IN.OperationalStatus == OperationalStatus.Up)
                    {
                        sharedForConnection = IN.Name;
                        break;
                    }
                }

                if (string.IsNullOrWhiteSpace(connectionToShare) || string.IsNullOrWhiteSpace(sharedForConnection))
                {
                    return(false);
                }

                foreach (INetConnection c in connections)
                {
                    var props = manager.NetConnectionProps[c];
                    INetSharingConfiguration tempSharingCfg = manager.INetSharingConfigurationForINetConnection[c];
                    if (props.Name == connectionToShare)
                    {
                        tempSharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
                        if (tempSharingCfg.SharingEnabled == true)
                        {
                            jShareWIFIRet = "已设置" + props.Name + "用于共享";
                            sharingCfg    = tempSharingCfg;
                        }
                    }
                    else if (props.Name == sharedForConnection)
                    {
                        tempSharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
                        priSharingCfg = tempSharingCfg;
                    }
                }


                jShareWIFIRet = "Success";
                return(true);
            }
            catch (Exception e)
            {
                jShareWIFIRet = e.Message;
                return(false);
            }
        }
 public bool EnableSharing(string connectionToShare, string sharedForConnection)
 {
     try
     {
         var manager = new NetSharingManager();
         var connections = manager.EnumEveryConnection;
         foreach (INetConnection c in connections)
         {
             var props = manager.NetConnectionProps[c];
             var sharingCfg = manager.INetSharingConfigurationForINetConnection[c];
             if (props.Name == connectionToShare)
             {
                 //MessageBox.Show(connectionToShare.ToString());
                 sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
             }
             else if (props.Name == sharedForConnection)
             {
                 //MessageBox.Show(sharedForConnection.ToString());
                 sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
             }
         }
         return true;
     }
     catch
     {
         return false;
     }
 }
 private async Task JShareWIFI(bool isShare)
 {
     try
     {
         NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
         string currentIP = "";
         if (isShare)
         {
             currentIP = Dns.GetHostAddresses(Dns.GetHostName()).GetValue(3).ToString();
         }
         else
         {
             currentIP = Dns.GetHostAddresses(Dns.GetHostName()).GetValue(2).ToString();
         }
         string currentNetwork = "";
         string shareNetwork = adapters[0].Name;
         foreach (NetworkInterface adapter in adapters)
         {
             IPInterfaceProperties ip = adapter.GetIPProperties();
             if (ip.UnicastAddresses[1].Address.ToString() == currentIP)
             {
                 currentNetwork = adapter.Name;
                 break;
             }
         }
         var manager = new NetSharingManager();
         var conn = manager.EnumEveryConnection;
         foreach (INetConnection netConn in conn)
         {
             var props = manager.NetConnectionProps[netConn];
             var sharingCfg = manager.INetSharingConfigurationForINetConnection[netConn];
             if (isShare)
             {
                 if (props.Name == currentNetwork)
                 {
                     sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
                 }
                 else if (props.Name == shareNetwork)
                 {
                     sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
                 }
             }
             else
             {
                 sharingCfg.DisableSharing();
             }
         }
     }
     catch (Exception e)
     {
         lblStatus.Content = lblStatus.ToolTip = "Operation fails, you must run this program as an administrator.";
         chkIsEnable.IsChecked = true;
     }
     progressBar.IsActive = false;
 }
Beispiel #11
0
 public NetSharing()
 {
     nsm = new NetSharingManager();
 }
Beispiel #12
0
 public NetworkConnection(INetConnection icsConnection)
 {
     _icsMgr  = new NetSharingManagerClass();
     _icsConn = icsConnection;
 }