【问题标题】:My.Computer.FileSystem.FileExists doesnt work [vb.net]My.Computer.FileSystem.FileExists 不起作用 [vb.net]
【发布时间】:2018-03-23 03:39:48
【问题描述】:

我不明白为什么我找不到文件,它在正确的位置。它以前工作过。但现在它只适用于同一位置的某些文件。我 100% 确定文件的路径和名称与代码中的相同.... 无法找到文件时无法理解...

 If ComboBox2.Text = "  1YZ-C01C 567.737.061 CA" Then
        If My.Computer.FileSystem.FileExists("source/Chr/1YZ-C01C 567.737.061-eeprom.txt") Then
            If My.Computer.FileSystem.FileExists("source/Chrysler/1YZ-C01C 567.737.061-erom.txt") Then
                Button2.Enabled = False
                button1.Enabled = False 
        Else
            ErrorOops.Show()
            Button2.Enabled = True
            Label1.Text = "Cant Find the file."
            Exit Sub
        End If
        Else
            ErrorOops.Show()
            Button2.Enabled = True
            Label1.Text = "Cant Find the file."
            Exit Sub
        End If
    End If

【问题讨论】:

    标签: vb.net file-exists


    【解决方案1】:

    您需要提供完整路径。 ("source/Chr/1YZ-C01C 567.737.061-eeprom.txt") 不是完整路径。

    用途:

    Path.Combine(Path, File)
    

    所以在您的示例文件中将是: "1YZ-C01C 567.737.061-eeprom.txt" 以及到 "source/Chr" 的完整路径(以 C:D: 或其他开头)

    典型示例:

    dim MyPath as string = "C:\MyData\source\Chr"
    dim MyPath as string = Application.StartupPath & "\source\Chr" 'Alternative
    dim MyFile as string = "1YZ-C01C 567.737.061-eeprom.txt"
    If My.Computer.FileSystem.FileExists(Path.Combine(MyPath,MyFile)) then
    ...
    end if
    

    【讨论】:

    • 否,source/Chr/1YZ-C01C 567.737.061-eeprom.txt - 表示 soruce 文件夹与 .exe 文件位于同一路径
    • 我编辑了上面的答案来举例。但是,我想我可能误解了原始问题,所以如果不相关请忽略。你是网络上的路径吗?
    • 在应用程序上运行调试时,应用程序文件夹为 ./bin/Debug 。所以你的文件路径是 /bin/Debug/source/Chr/1YZ-C01C 567.737.061-eeprom.txt .
    【解决方案2】:

    您需要提供完整路径。当文件与您的应用程序位于同一文件夹中时,您可以使用Application.StartupPath。所以它看起来像这样:

    If My.Computer.FileSystem.FileExists(Application.StartupPath & "/source/Chr/1YZ-C01C 567.737.061-eeprom.txt") Then
    

    如果您的应用程序路径例如是 C:\test,那么结果是:C:\test/source/Chr/1YZ-C01C 567.737.061-eeprom.txt

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 2013-12-19
      • 1970-01-01
      相关资源
      最近更新 更多