private NetworkDetails GetNetworkFromUi()
        {
            if (Guid.TryParse(txtNetworkId.Text, out Guid idAsGuid))
            {
                NetworkDetails networkDetails = new NetworkDetails()
                {
                    Id          = $"{{{idAsGuid.ToString().ToUpper()}}}",
                    ProfileName = txtNetworkProfileName.Text,
                    Description = txtNetworkDescription.Text,
                    Category    = (cmbNetworkCategory.SelectedItem as NetworkCategoryKind?) ?? NetworkCategoryKind.Private,
                    NameType    = (int)nudNetworkNameType.Value
                };

                if (string.IsNullOrWhiteSpace(networkDetails.Id))
                {
                    return(null);
                }

                return(networkDetails);
            }
            else
            {
                return(null);
            }
        }
        private void lstNetworks_SelectedValueChanged(object sender, EventArgs e)
        {
            // Load info for a network
            //

            NetworkDetails selectedNetwork = lstNetworks.SelectedItem as NetworkDetails;

            ShowNetwork(selectedNetwork);
        }
        private void ShowNetwork(NetworkDetails networkDetails)
        {
            txtNetworkId.Text               = networkDetails?.Id ?? "";
            txtNetworkProfileName.Text      = networkDetails?.ProfileName ?? "";
            txtNetworkDescription.Text      = networkDetails?.Description ?? "";
            cmbNetworkCategory.SelectedItem = networkDetails?.Category ?? NetworkCategoryKind.Private;
            nudNetworkNameType.Value        = networkDetails?.NameType ?? 0;

            lblSaveResult.Text = "";
        }
Example #4
0
        public static void SaveNetworkDetails(NetworkDetails networkDetails, RegistryKey networksKey)
        {
            using (RegistryKey networkKey = networksKey.OpenSubKey(networkDetails.Id, RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                if (networkKey == null)
                {
                    throw new InvalidOperationException("Could not open network registry key.");
                }

                // Save all network information
                //
                networkKey.SetValue("ProfileName", networkDetails.ProfileName);
                networkKey.SetValue("Description", networkDetails.Description);
                networkKey.SetValue("Category", (int)networkDetails.Category);
                networkKey.SetValue("NameType", networkDetails.NameType);
            }
        }
Example #5
0
        public static void SaveNetworkDetails(NetworkDetails networkDetails)
        {
            // Make sure we open 64 bit registry so we don't get stuck in Wow32Node
            //
            using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
            {
                using (RegistryKey networksKey = hklm.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkList\\Profiles", RegistryKeyPermissionCheck.ReadSubTree))
                {
                    if (networksKey == null)
                    {
                        throw new InvalidOperationException("Could not open network list registry key.");
                    }

                    SaveNetworkDetails(networkDetails, networksKey);
                }
            }
        }
        private void btnNetworkSave_Click(object sender, EventArgs e)
        {
            NetworkDetails details = GetNetworkFromUi();

            bool   saved = false;
            string error = null;

            if (details != null)
            {
                try
                {
                    SaveNetworkDetails(details);
                    saved = true;
                }
                catch (Exception ex)
                {
                    error = ex.Message;
                }
            }
            else
            {
                return;
            }

            RefreshNetworks();

            NetworkDetails newDetails = lstNetworks.Items.OfType <NetworkDetails>().FirstOrDefault(n => n.Id == details.Id);

            lstNetworks.SelectedItem = newDetails;

            if (saved)
            {
                lblSaveResult.Text = "Saved.";
            }
            else if (error != null)
            {
                lblSaveResult.Text = error;
            }
        }
Example #7
0
        public static NetworkDetails GetNetworkDetails(string name, RegistryKey networksKey)
        {
            using (RegistryKey networkKey = networksKey.OpenSubKey(name))
            {
                if (networkKey == null)
                {
                    throw new InvalidOperationException("Could not open network registry key.");
                }

                // Extract all network information
                //
                NetworkDetails networkDetails = new NetworkDetails()
                {
                    Id          = name,
                    ProfileName = networkKey.GetValue("ProfileName") as string,
                    Description = networkKey.GetValue("Description") as string,
                    Category    = ((NetworkCategoryKind?)(networkKey.GetValue("Category") as int?)) ?? NetworkCategoryKind.Private,
                    NameType    = (networkKey.GetValue("NameType") as int?) ?? 0
                };

                return(networkDetails);
            }
        }
        private void btnNetworkDelete_Click(object sender, EventArgs e)
        {
            NetworkDetails details = GetNetworkFromUi();

            bool   deleted = false;
            string error   = null;

            if (details != null)
            {
                try
                {
                    DeleteNetwork(details);
                    deleted = true;
                }
                catch (Exception ex)
                {
                    error = ex.Message;
                }
            }
            else
            {
                return;
            }

            RefreshNetworks();
            lstNetworks.SelectedItem = null;

            if (deleted)
            {
                lblSaveResult.Text = "Deleted.";
            }
            else if (error != null)
            {
                lblSaveResult.Text = error;
            }
        }
Example #9
0
 public static void DeleteNetwork(NetworkDetails networkDetails, RegistryKey networksKey)
 {
     networksKey.DeleteSubKeyTree(networkDetails.Id, true);
 }