如何得知本地IP是DHCP获得的还是静态的?

minioreo 2008-01-09 12:40:19
如题 Dns.GetHostEntry(Dns.GetHostName())只能得到IP地址 想知道是静态设置的还是DHCP分配的
...全文
528 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
minioreo 2008-01-09
  • 打赏
  • 举报
回复
改了一下可以了 原来StandardInput.WriteLine写到了cmd里 而第一个netsh以后启动了netsh.exe 而以后的都写到cmd里头 netsh没有接收到
还好用批处理模拟了下才发现 再次感谢 呵呵
Process p = new Process();
p.StartInfo.FileName = "netsh";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.OutputDataReceived += delegate(object Osender, DataReceivedEventArgs arg)
{
label2.Text += arg.Data + "\n";
};
p.ErrorDataReceived += delegate(object Osender, DataReceivedEventArgs arg)
{
label2.Text += arg.Data + "\n";
};
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.StandardInput.WriteLine("interface ip");
p.StandardInput.WriteLine(ConfigOfIP);
p.StandardInput.WriteLine();
p.StandardInput.WriteLine(ConfigOfDNS);
p.Exited+= delegate(object sender,EventArgs e)
{
MessageBox.Show("finished");
};
p.StandardInput.WriteLine("exit");
p.WaitForExit();
label1.Text = GetMyIp();
button1.Enabled = true;
effun 2008-01-09
  • 打赏
  • 举报
回复
很久以前在批处理里用netsh改过地址,不过没有用Process试过。
minioreo 2008-01-09
  • 打赏
  • 举报
回复
有没有试过用process调用netsh的?怎么不能退出呢?

   
ConfigOfIP = "set address \"本地连接\" static 192.168.1.15 255.255.255.0 192.138.1.1 1“;
ConfigOfDNS = "set dns \"本地连接\" dhcp";

Process p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.OutputDataReceived += delegate(object Osender, DataReceivedEventArgs arg)
{
label2.Text += arg.Data + "\n";
};
p.ErrorDataReceived += delegate(object Osender, DataReceivedEventArgs arg)
{
label2.Text += arg.Data + "\n";
};
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.StandardInput.WriteLine("netsh");
p.StandardInput.WriteLine("interface ip");
p.StandardInput.WriteLine(ConfigOfIP);
p.StandardInput.WriteLine(ConfigOfDNS);
p.StandardInput.WriteLine("exit");
p.StandardInput.WriteLine("exit");
p.WaitForExit();
label1.Text = GetMyIp();
minioreo 2008-01-09
  • 打赏
  • 举报
回复
OK 解决了 谢谢一楼
NetworkInterface[] adapter = NetworkInterface.GetAllNetworkInterfaces();
IPInterfaceProperties ipp = adapter[0].GetIPProperties();
IPv4InterfaceProperties ip4ip = ipp.GetIPv4Properties();
if (ip4ip.IsDhcpEnabled)
radioButton1.Checked = true;
else
radioButton2.Checked = true;
lextm 2008-01-09
  • 打赏
  • 举报
回复
直接分析注册表,在HKEY_LOCAL_MACHINE\System\ControlSet001\Services下面,带有{}的就是网卡相关项,读取Parameters下面的Tcpip的子项做分析就好了,EnableDHCP的值对应于是不是开启了DHCP。

.NET BCL似乎并没有特别的封装这一部分内容。
changjiangzhibin 2008-01-09
  • 打赏
  • 举报
回复
看一下相关文档
effun 2008-01-09
  • 打赏
  • 举报
回复
System.Net.NetworkInformation 命名空间

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧