如何在Windows服务或控制台应用程序中更改系统IP

时间:2012-12-18 21:50:53

标签: c# windows windows-services ip ip-address

我正在编写C#应用程序并想要更改系统的IP地址。

我找到了这个命令

netsh interface ip set address name="Local Area Connection" static 192.168.1.191 255.255.255

但我怎么能在代码中做到这一点?

1 个答案:

答案 0 :(得分:3)

这样的事情应该可以使用System.Diagnotics.Process类。

string result;

Process p = new Process();
p.StartInfo.FileName = "netsh.exe";
p.StartInfo.Arguments = "interface ip add address name=\"Local Area Connection\" static 192.168.1.191 255.255.255";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
try
{
   p.Start();
   p.WaitForExit(30000);
   result = p.StandardOutput.ReadToEnd();
}
catch(Exception ex)
{
   result = ex.Message;
}

// check "result" variable for your results.
相关问题