【问题标题】:Conversion from String to Long not valid从 String 到 Long 的转换无效
【发布时间】:2017-11-01 11:50:00
【问题描述】:

我正在创建一个 VB Windows 表单,它检索在 AD 中注册的计算机列表,然后检查每台计算机是否存在应用程序(查找 .exe)。我在连接 .exe 的路径时遇到问题我收到错误“从字符串到长的对话无效”

我该如何解决这个问题?

Private Sub CheckButton_Click(sender As Object, e As EventArgs) Handles CheckButton.Click

    Dim PCsource As String = "C:\FilePath\adComputers.txt"
    Dim FilepathX64 As String = "\C$\Program Files (x86)\FilePath\Some.exe"
    Dim FilepathX86 As String = "\C$\Program Files\FilePath\Some.exe"
    Dim Check As String
    Dim Install As System.IO.StreamWriter
    Dim Installed As System.IO.StreamWriter

    If System.IO.File.Exists(PCsource) = True Then
        Dim Computer As New System.IO.StreamReader(PCsource)
        Do While Computer.Peek() <> -1
            Check = Computer.ReadLine()
            If System.IO.File.Exists("\\" & Check & FilepathX64 Or "\\" & Check & FilepathX86) = True Then
                Installed = My.Computer.FileSystem.OpenTextFileWriter("C:\FilePath\Installed.txt", True)
                Installed.WriteLine("Sophos is installed on " & Check, True)
                Installed.Close()
            Else
                Install = My.Computer.FileSystem.OpenTextFileWriter("C:\FilePath\Install.txt", True)
                Install.WriteLine(Check, True)
                Install.Close()
            End If
        Loop
    Else
        FailSound.Play()
        MessageBox.Show("The Domain Computer List is missing")
        Exit Sub
    End If

End Sub

【问题讨论】:

  • 请添加错误消息的确切文本,其中还应包括有关哪条线路失败的信息。
  • 你真的不应该像在编译程序中那样硬编码值。你有什么理由不能使用像 PowerShell 这样非常适合这类事情的脚本语言?
  • 您绝对应该探索 powershell 选项。它正是为这样的东西而制作的。
  • 一个字符串和一个长在一个酒吧...
  • 错误:从字符串“\\COMPUTERNAME\C$\Program Fil”到类型“Long”的转换无效

标签: vb.net file active-directory


【解决方案1】:

你不能这样做。

if System.IO.File.Exists("\\" & Check & FilepathX64 Or "\\" & Check & FilepathX86) = True then 

但你可以这样做

if (System.IO.File.Exists("\\" & Check & FilepathX64) = True) or (System.IO.File.Exists("\\" & Check & FilepathX86) = True) then

using file.exist

【讨论】:

  • 谢谢你完美的 MutedDisk.... 尝试投票但没有足够的 Rep
  • @RootUser 没关系,您已经接受了答案。有一个好芽
  • 您可能希望将Or 更改为OrElse,这样如果找到第一个文件,代码将正确短路。此代码将检查这两个文件,即使找到第一个文件也是如此。如果找到第一个,则不需要第二个检查。
  • @ChrisDunaway 好点。因为他玩的是 64 位和 32 位环境,所以实际上应该是 if elseif else end if。根据您的操作,可以通过多种方式获得相同的结果。
【解决方案2】:

如果有人有兴趣,一个简单的 PowerShell 方法:

Get-ADComputer -Filter * |
    ForEach-Object {
        if (Test-Path "\\$($_.Name)\c$\Program Files\FilePath\Some.exe" -or
            Test-Path "\\$($_.Name)\c$\Program Files (x86)\FilePath\Some.exe")
        { "Sophos is installed on $($_.Name)" }
        else { "Sophos is not installed on $($_.Name)" }
    }

还有很多改进的余地,使用Test-Connection 来减少离线计算机或将计算机限制在原始示例中的特定列表中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多