0

I'm making a WinForm C# .NET 6.0 application which can retrieve actual network configuration and modify it if user want to.

And i'm stuck on the network modify part.

So what type of code i need to enter inside the change_infos region ?

I already check some website but every time i try to use their codes and modify them to mine, the network configuration doesn't change.

[edit] the code is the following

using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Diagnostics;

namespace NUTs___Network_Utility_Tools
{
    public partial class MainForm : Form
{

    public
        IPAddressCollection
        DnsAddresses
    { get; }

    public string ipAddress;
    public string subnetMask;
    public string defGateway;
    public string primDNS;
    public string secDNS;


    public MainForm()
    {
        InitializeComponent();
        switchtoen_menuBtn.Enabled = false;

    }



    private void chng_FR(object sender, EventArgs e)
    {
        #region form_Content
        this.Text = "NUTs - un utilitaire réseau";
        language_menuItem.Text = "Langue";
        switchtofr_menuBtn.Text = "Passer en français";
        switchtoen_menuBtn.Text = "Passer en anglais";
        switchtofr_menuBtn.Enabled = false;
        switchtoen_menuBtn.Enabled = true;
        #endregion

        #region chng_FR_Tab
        networkinfos_tabPage.Text = "Vos informations réseau";
        changeparameters_tabPage.Text = "Changer vos paramètres réseau";
        ip_richTxtBx1.Text = "Adresse IP";
        ip_richTxtBx2.Text = "Adresse IP";
        subnetmask_richTxtBx1.Text = "Masque de sous réseau";
        subnetmask_richTxtBx2.Text = "Masque de sous réseau";
        defgateway_richTxtBx1.Text = "Passerelle par défaut";
        defgateway_richTxtBx2.Text = "Passerelle par défaut";
        primdns_richTxtBx1.Text = "Adresse DNS primaire";
        primdns_richTxtBx2.Text = "Adresse DNS primaire";
        secdns_richTxtBx1.Text = "Adresse DNS secondaire";
        secdns_richTxtBx2.Text = "Adresse DNS secondaire";
        retrieveinfos_Btn.Text = "RECUPERER INFOS";
        applyparams_Btn.Text = "APPLIQUER PARAMETRES";
        ip_TxtBx1.Text = "";
        subnetmask_TxtBx1.Text = "";
        defgateway_TxtBx1.Text = "";
        primdns_TxtBx1.Text = "";
        secdns_TxtBx1.Text = "";
        #endregion
    }

    private void chng_EN(object sender, EventArgs e)
    {
        #region form_Content
        this.Text = "NUTs - a network utility tool";
        language_menuItem.Text = "Language";
        switchtofr_menuBtn.Text = "Switch to french";
        switchtoen_menuBtn.Text = "Switch to english";
        switchtofr_menuBtn.Enabled = true;
        switchtoen_menuBtn.Enabled = false;
        #endregion

        #region chng_EN_Tab
        networkinfos_tabPage.Text = "Your network informations";
        changeparameters_tabPage.Text = "Change your network parameters";
        ip_richTxtBx1.Text = "IP Address";
        ip_richTxtBx2.Text = "IP Address";
        subnetmask_richTxtBx1.Text = "Subnet mask";
        subnetmask_richTxtBx2.Text = "Subnet mask";
        defgateway_richTxtBx1.Text = "Default gateway";
        defgateway_richTxtBx2.Text = "Default gateway";
        primdns_richTxtBx1.Text = "Primary DNS address";
        primdns_richTxtBx2.Text = "Primary DNS address";
        secdns_richTxtBx1.Text = "Secondary DNS address";
        secdns_richTxtBx2.Text = "Secondary DNS address";
        retrieveinfos_Btn.Text = "RETRIEVE INFOS";
        applyparams_Btn.Text = "APPLY PARAMETERS";
        ip_TxtBx1.Text = "";
        subnetmask_TxtBx1.Text = "";
        defgateway_TxtBx1.Text = "";
        primdns_TxtBx1.Text = "";
        secdns_TxtBx1.Text = "";
        #endregion
    }

    #region retrieve_infos
    private void retrieve_Infos(object sender, EventArgs e)
    {
        foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && adapter.OperationalStatus == OperationalStatus.Up || adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && adapter.OperationalStatus == OperationalStatus.Up)
            {
                foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        ip_TxtBx1.Text = (ip.Address.ToString());
                        subnetmask_TxtBx1.Text = (ip.IPv4Mask.ToString());
                    }
                }
                foreach (GatewayIPAddressInformation defgateway in adapter.GetIPProperties().GatewayAddresses)
                {
                    defgateway_TxtBx1.Text = (defgateway.Address.ToString());
                }
            }
        }
        foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
        {
            IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
            IPAddressCollection dnsServers = adapterProperties.DnsAddresses;

            if (adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && adapter.OperationalStatus == OperationalStatus.Up || adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && adapter.OperationalStatus == OperationalStatus.Up)
            {
                foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
                {
                    if (dnsServers.Count > 0 && ip.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        foreach (IPAddress dns in dnsServers)
                        {
                            primdns_TxtBx1.Text = dnsServers[0].ToString();
                            secdns_TxtBx1.Text = dnsServers[1].ToString();
                        }
                    }
                }
            }
        }
    }
    #endregion

    #region change_infos
    private void apply_parameters(Button sender, EventArgs e, string arg)
    { 
        foreach (NetworkInterface Ethadapter in NetworkInterface.GetAllNetworkInterfaces())
        {

            if (Ethadapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && Ethadapter.OperationalStatus == OperationalStatus.Up)
            {
                ipAddress = ip_richTxtBx2.Text;
                subnetMask = subnetmask_TxtBx2.Text;
                defGateway = defgateway_TxtBx2.Text;
                primDNS = primdns_TxtBx2.Text;
                secDNS = secdns_TxtBx2.Text;

                ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
                psi.UseShellExecute = true;
                psi.WindowStyle = ProcessWindowStyle.Hidden;
                psi.Verb = "runas";
                psi.Arguments = ("/c netsh interface ip set address \"" + Ethadapter + "\" static " + ipAddress + " " + subnetMask + " " + primDNS + " & netsh interface ip set dns \"" + Ethadapter + "\" static " + primDNS);
                Process.Start(psi);
            }
        }
    }
    #endregion
}

}

L-Dlbcq
  • 1
  • 2
  • summary, you have code to change some network settings , that code doesnt work. But you dont show the code you have that doesnt work, how are we supposed to work out what you did wrong – pm100 Mar 12 '22 at 19:17
  • anyway WMI is the way to go like here https://stackoverflow.com/questions/209779/how-can-you-change-network-settings-ip-address-dns-wins-host-name-with-code – pm100 Mar 12 '22 at 19:19
  • i already try using this but nothing changed. May i used it bad – L-Dlbcq Mar 13 '22 at 20:25

0 Answers0