程序在C:\但不是E:\

时间:2018-06-13 06:36:30

标签: vb.net

我在VB 2017中编写了一个简单的程序,用于收集有关我的计算机的Internet连接的数据,以便在以后的脚本中使用,以尝试解决ip问题。该程序应该从我的桌面运行,该桌面位于我的E:\ Drive而不是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文件。命令提示符似乎运行正常。不确定是什么。

3 个答案:

答案 0 :(得分:1)

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

提供"路径"的实际价值可能会很好。和" path1"。

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

答案 1 :(得分:1)

        .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 " & "ipconfig /all " & "> " & path1

另外,我会按照上面的建议考虑空间可能在路径中。

答案 2 :(得分:0)

按照@AlessandroMandelli和@wech的提示...我写了一个(太长的)程序的附加部分,将引号放入路径,确保空格不会搞砸程序:

    '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

并且......它有效!该程序现在产生的结果与没有引号的路径相同!最终产品/项目可以查看here