Vb.net 在C:\上工作的程序,但不是E:\

Vb.net 在C:\上工作的程序,但不是E:\,vb.net,Vb.net,我用VB 2017编写了一个简单的程序,旨在收集有关我的计算机互联网连接的数据,以便在以后的脚本中使用,试图修复ip问题。该程序将从我的桌面上运行,该桌面位于我的E:\驱动器上,而不是主驱动器(C:)上的C:\上。该程序运行良好,并将所需信息输出到文本文件中。但在E:,该程序仅创建文本文件,但将其保留为空。代码如下: Imports System.IO Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventA

我用VB 2017编写了一个简单的程序,旨在收集有关我的计算机互联网连接的数据,以便在以后的脚本中使用,试图修复ip问题。该程序将从我的桌面上运行,该桌面位于我的E:\驱动器上,而不是主驱动器(C:)上的C:\上。该程序运行良好,并将所需信息输出到文本文件中。但在E:,该程序仅创建文本文件,但将其保留为空。代码如下:

Imports System.IO

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Creates path for output file & cmd process
    Dim path As String = My.Application.Info.DirectoryPath() & "\ExIpOutput.txt"
    Dim path1 As String = My.Application.Info.DirectoryPath() & "\Ipconfig.txt"
    Dim pathSub As String = path.Substring(2, 3)
    Dim path1Sub As String = path1.Substring(2, 3)

    'Creates output files
    Dim fs As FileStream = File.Create(path)
    fs.Close()
    Dim fs1 As FileStream = File.Create(path1)
    fs1.Close()

    'Declares the ExIP Program
    Dim psi As New ProcessStartInfo With {
        .Verb = "runas",
        .FileName = "Cmd",
        .Arguments = "/c" & "nslookup myip.opendns.com. resolver1.opendns.com" & "> " & path
    }
    'Attempts to run the program
    Process.Start(psi)

    'Declares the IPConf Program
    Dim psi1 As New ProcessStartInfo With {
        .Verb = "runas",
        .FileName = "Cmd",
        .Arguments = "/c" & "ipconfig /all" & "> " & path1
    }
    'Attempts to run the program
    Process.Start(psi1)

End Sub
End Class
我尝试/调试的内容:


我最初认为这是路径的问题,是
E:\\
而不是
E:\
,但是在编写了一个令人烦恼的复杂函数来修复路径之后,同样的结果出现了。我在两个具有管理权限的位置运行,并且使用相同的.exe文件。命令提示符似乎运行正常。不确定发生了什么。

您正在将nslookup和ipconfig的输出重定向到一个不带引号的路径,因此您在驱动器E:\上的位置可能包含空格,这将阻止nslookup写入正确的文件

也许最好提供“path”和“path1”的实际值

如果不是这样,请尝试以管理员身份打开命令提示符,并尝试使用相同的路径执行完全相同的命令,以验证其是否有效

        .Arguments = "/c" & "nslookup myip.opendns.com. resolver1.opendns.com" & "> " & path
看起来更像

        .Arguments = "/c " & "nslookup myip.opendns.com resolver1.opendns.com " & "> " & path
        .Arguments = "/c " & "ipconfig /all " & "> " & path1

看起来更像

        .Arguments = "/c " & "nslookup myip.opendns.com resolver1.opendns.com " & "> " & path
        .Arguments = "/c " & "ipconfig /all " & "> " & path1

我也会遵循上面的建议,考虑空间可能在路径中。

< P>跟随AlessandroMandelli和WeWeh的提示。我编写了一个(太长)的程序附加部分,将引号放入路径中,以确保空格不会使程序出错:

    'Appends path1 string with quotes to fix spacing
    Dim pathIns As String = """"
    Dim path1F As String = path1.Insert(0, pathIns)
    path1 = path1F

    Dim path1FIns As Integer = path1.Length()
    Dim path1FF As String = path1.Insert(path1FIns, pathIns)
    path1 = path1FF

    'Appends path string...
    Dim pathF As String = path.Insert(0, pathIns)
    path = pathF

    Dim pathFIns As Integer = path.Length()
    Dim pathFF As String = path.Insert(pathFIns, pathIns)
    path = pathFF

和。。成功了!该程序现在产生的结果与不带引号的路径相同!可以查看最终产品/项目

我们不是调试服务。您先进行调试,如果仍然无法解决问题,请向我们提供调试时收集的所有相关信息。@JMCILHINEY将编辑我已调试了几天该程序是为在其他位置实现而编写的,因此路径的“精确值”可能会更改,这就是为什么我使用变量而不是精确值,好建议。我确实认为这可能是问题所在,但我不知道如何解决它:/我想我可能已经找到了解决办法。在命令提示符中引用字符串之前,我可以在字符串的开头和结尾都加上引号,这样可以防止路径中的空格破坏命令。如果/当它起作用时,我会发布一个正式的答案。我不确定这会有多大影响,但任何一点都会有帮助:)谢谢!