2

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

我找到了这个命令

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

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

4

1 回答 1

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.
于 2012-12-18T22:12:06.140 回答