Problemi a settare l'indirizzo IP

venerdì 30 gennaio 2004 - 10.28

angela Profilo | Newbie

Ho creato una web form che dovrebbe cambiare l'indirizzo ip, la subnet mask ed il gateway della macchina con i nuovi valori che inserisco in delle textBox.
Il mio problema è che non varia nessun valore!
Ho provato a fare il tutto con una win form è funziona correttamente, come mai?
Metto di seguito il codice così vedete come ho fatto e vi prego di aiutarmi!!
Grazie!


private void Page_Load(object sender, System.EventArgs e)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();

foreach(ManagementObject objMO in objMOC)
{
if(!(bool)objMO["ipEnabled"]) continue;
string[] ipaddresses = (string[]) objMO["IPAddress"];
string[] subnets = (string[]) objMO["IPSubnet"];
string[] gateways = (string[]) objMO["DefaultIPGateway"];
}

}

public void setrete(string IPAddress,string SubnetMask, string Gateway)
{
if((IPAddress!="") && (SubnetMask!="") && (Gateway!="")) {
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();

foreach(ManagementObject objMO in objMOC)
{
if (!(bool) objMO["IPEnabled"]) continue;
try
{
ManagementBaseObject objNewIP = null;
ManagementBaseObject objSetIP = null;
ManagementBaseObject objNewGate = null;
objNewIP = objMO.GetMethodParameters("EnableStatic");
objNewGate = objMO.GetMethodParameters("SetGateways");
objNewGate["DefaultIPGateway"] = new string[] {Gateway};
objNewGate["GatewayCostMetric"] = new int[] {1};
objNewIP["IPAddress"] = new string[] {IPAddress};
objNewIP["SubnetMask"] = new string[] {SubnetMask};
objSetIP = objMO.InvokeMethod("EnableStatic",objNewIP,null);
objSetIP = objMO.InvokeMethod("SetGateways",objNewGate,null);
Label1.Text= "Impostazioni di rete modificate correttamente ";
}
catch(Exception ex)
{
Label1.Text= "Non è stato possibile modificare le impostazioni della scheda di rete : " + ex.Message;
}
}
}
else {
Label1.Text="Valori impostati non validi";
}

private void settarete_Click(object sender, System.EventArgs e)
{
string ipaddress, subnet, gateway;
ipaddress=ip.Text+"."+ip2.Text+"."+ip3.Text+"."+ip4.Text;
gateway=gw.Text;
subnet=sb.Text;
setrete(ipaddress,subnet,gateway);
}

Brainkiller Profilo | Guru

Mi pare stai usando WMI per modificare le impostazioni vero?
Beh, forse dall'applicazione Web Form non è possibile perchè viene eseguita con l'utente particolare \ASPNET che magari non può fare certe cose (nel ns. caso cambiare l'IP di un'altra macchina).
Secondo me potresti tentare di attivare l'impersonation di ASP.NET e simulare un utente vero e proprio tipo l'Administrator della macchina e vedere se te li cambia.
Mentre per l'applicazione WinForm l'utente con cui gira è quello loggato in quel momento (ti ricordo che l'utente ASPNET è molto particolare).

ciao
david
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5