It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center




已关闭8年。




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

我找到了这个命令
netsh interface ip set address name="Local Area Connection" static 192.168.1.191 255.255.255

但是,如何在代码中执行此操作?

最佳答案

这样的事情应该可以使用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.

关于c# - 如何在Windows服务或控制台应用程序中有问题地更改系统IP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13942062/

10-13 06:51