示例#1
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);
            }
        }
示例#2
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;
     }
 }
示例#3
0
 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;
 }