【问题标题】:How to obtain ip address automatically programmatically如何以编程方式自动获取IP地址
【发布时间】:2013-12-04 11:09:41
【问题描述】:
Dim args As String
args="netsh wlan interface ip set address """+adptrname+""" dhcp"
Dim proc As New System.Diagnostics.Process()        
proc.StartInfo.FileName = "netsh"
proc.StartInfo.Verb="RunAs"
proc.StartInfo.CreateNoWindow = true
proc.StartInfo.RedirectStandardOutput = True
proc.StartInfo.Arguments = args
proc.StartInfo.UseShellExecute = False
proc.Start()
My.Computer.FileSystem.WriteAllText(MainForm.filePath,proc.StandardOutput.ReadToEnd(), True)
proc.Close()
MsgBox(args)

我编写了上面的代码来更改我的 wlan 适配器以自动获取 ip 地址,但它仍然保留在旧的 ip 配置中,尽管输出表明启用了 dhcp。有人可以告诉我哪里出错了。

【问题讨论】:

  • 只是一个疯狂的猜测:也许 args 不应该包含命令本身的名称。尝试在 args 中没有“netsh”的代码。如果这没有帮助,请尝试读出 StandardError 并查看那里的内容。通常您也不必关闭进程。事实上,它可能会破坏它的功能。它应该能够自行关闭。改用 proc.WaitForExit()。

标签: vb.net ip wmi ip-address


【解决方案1】:

好的,我尝试在 cmets 上执行此操作,但我不断发现错误...所以这里是您的代码的更正版本:

'Where does your adptrname come from?
Dim adptrname As String = "wlan0" 'i.E.

Dim args As String
' Note that i removed 'netsh' from args
' And the notation to add the addapter name (in VB we use & instead of +)
args="netsh int ipv4 set address name=""" & adptrname & """ source=dhcp"
Dim proc As New System.Diagnostics.Process()        
proc.StartInfo.FileName = "netsh"
proc.StartInfo.Verb="RunAs"
proc.StartInfo.CreateNoWindow = true
proc.StartInfo.RedirectStandardOutput = True
proc.StartInfo.RedirectStandardError = True ' maybe you want to read this, too
proc.StartInfo.Arguments = args
proc.StartInfo.UseShellExecute = False
proc.Start()

' This is still bad, but we keep it for now
My.Computer.FileSystem.WriteAllText(MainForm.filePath, proc.StandardOutput.ReadToEnd(), True)
My.Computer.FileSystem.WriteAllText(MainForm.filePath, proc.StandardError.ReadToEnd(), True)


' Normally not necessery, but no way we do proc.close() here
proc.WaitForExit()

' ok, this shoud have shown you until now that it didn't work...
MsgBox(args)

阅读 cmets,尝试对您自己的代码进行更正,看看它是否运行。

编辑参数

我也认为它不适用于你的论点......

试试这样吧:

netsh int ipv4 set address name="Wireless Connection" source=dhcp

我根据帮助构建了它,它可以在我的机器上运行 (Win7x64-en_US)

【讨论】:

  • 在您的 cmd 中输入 netsh int ipv4 set dns 并查看帮助信息。我的意思是我要复制粘贴它但是......你知道
猜你喜欢
  • 2018-05-28
  • 1970-01-01
  • 2016-07-12
  • 2023-03-27
  • 2011-10-12
  • 2019-07-10
  • 2014-12-11
  • 2020-05-06
  • 2010-09-14
相关资源
最近更新 更多