Exemplo n.º 1
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);
        }