private void FormMain_Load(object sender, EventArgs e) { try { _netsh = new NetShareHost(); } catch (Exception ex) { MessageBox.Show(ex.Message, "NetShare", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); return; } if (!CheckServices()) { Application.Exit(); return; } Configuration conf = new Configuration(); conf.Read(); txtSSID.Text = _netsh.GetConnectionSettings().SSID; txtPassword.Text = _netsh.GetPassword(); var conns = _netsh.GetSharableConnections(); foreach (var item in conns) { cmbShareAdapter.Items.Add(item); } SharableConnection sharedConnection = null; sharedConnection = (from c in conns where c.Guid == conf.SharedConnection select c).FirstOrDefault(); if (sharedConnection == null) { sharedConnection = conns.First(); } for (int i = 0; i < cmbShareAdapter.Items.Count; i++) { if ((cmbShareAdapter.Items[i] as SharableConnection).Guid == sharedConnection.Guid) { cmbShareAdapter.SelectedIndex = i; break; } } chckIsAutostart.Checked = conf.IsAutostart; UpdateControls(); UpdateConnectionsListSafe(); _netsh.ConnectionsListChanged += _netsh_OnConnectionsListChanged; _netsh.HostedNetworkStarted += _netsh_HostedNetworkStarted; _netsh.HostedNetworkStopped += _netsh_HostedNetworkStopped; }
void LoadSavedState() { try { if (File.Exists(strStateFileName)) { VirtualRouterServiceState state; using (var file = new StreamReader(strStateFileName)) { var serializer = new DataContractSerializer(typeof(VirtualRouterServiceState)); state = (VirtualRouterServiceState)serializer.ReadObject(file.BaseStream); file.Close(); } if (state != null) { if (state.IsStarted) { // Set SSID and Password this.virtualRouterHost.SetConnectionSettings(state.SSID, state.MaxPeerCount); this.virtualRouterHost.SetPassword(state.Password); // Get the Shared Connection var conns = this.virtualRouterHost.GetSharableConnections(); SharableConnection sharedConnection = null; if (!string.IsNullOrEmpty(state.SharedConnectionGuid)) { sharedConnection = (from c in conns where c.Guid.ToString() == new Guid(state.SharedConnectionGuid).ToString() select c).FirstOrDefault(); if (sharedConnection == null) { sharedConnection = conns.First(); } } this.virtualRouterHost.Start(sharedConnection); } } } } catch (Exception ex) { WriteLog("Error Loading Service State - " + ex.ToString()); } }
static void Main(string[] args) { wlanEnabler enabler = new wlanEnabler(); VirtualRouterHost.IVirtualRouterHost host; host = new VirtualRouterHost.VirtualRouterHost(); host.SetPassword("egordasha"); host.SetConnectionSettings("User-AP", 10); IEnumerable <SharableConnection> list = host.GetSharableConnections(); SharableConnection ics = (from c in list where c.Name.Contains("vpnclient") select c).First(); if (host.Start(ics) && host.SetIP("2.0.4.1")) { Console.ReadLine(); } }