Bonsoir,
J'ai développé une application permettant de récupérer les infos réseau de la carte Ethernet ou Wifi active (Visible dans la région retrieve_infos). Suite à la demande de mon professeur de réseau (je ne suis pas du tout en cursus développement, mais je découvre les joies et les tristesses du dev), j'essaie d'intéger une partie modification de la configuration réseau.
Ma structure est clairement mauvaise du fait que je n'arrive pas à exécuter l'Invite de commande avec les arguments définis. Tout la partie dont je parle se situe dans la région apply_parameters.
J'ai commencé à lire le cours mais je vous avoue ne pas trop comprendre celui-ci. Et dans mon programme actuel, c'est beaucoup de copier-coller de code déjà existant que j'ai, tant bien que mal, essayé d'adapter.
Mon code actuel est le suivant:
Je ne demande pas que l'on me donne du code tout fait mais seulement des pistes vers lesquels fouiller.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 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 } }
Partager