【问题标题】:Directory.GetFiles Method Not Working In VBDirectory.GetFiles 方法在 VB 中不起作用
【发布时间】:2020-04-10 23:46:03
【问题描述】:

我使用 Visual Builder 创建了清理桌面的软件。我使用 Directory.GetFiles 方法将文件类型移动到某些目录中。当我第一次编码时,它运行良好,尽管随后我收到一条错误消息System.IO.IOException: 'Cannot create a file when that file already exists.,我不确定如何修复,因为我为带有单独按钮的文件创建目录,如代码中所示。

我的其他按钮也有问题,这可能是其他错误的结果。当我去清理我编写的将 .lnk 文件移动到快捷方式文件夹中的快捷方式时,除非它们以前位于该文件夹中,否则它们都不会移动到该文件夹​​中。

完整代码

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)
        MessageBox.Show("Desktop Cleaned")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim filePaths = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.png")

        For Each filePath In filePaths
            Dim filename = IO.Path.GetFileName(filePath)
            Dim newPath = IO.Path.Combine("C:\Users\bj\Desktop\Pictures", filename)

            IO.File.Move(filePath, newPath)

        Next filePath

        Dim filePaths2 = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.jpg")

        For Each filePath2 In filePaths2
            Dim filename2 = IO.Path.GetFileName(filePath2)
            Dim newPath2 = IO.Path.Combine("C:\Users\bj\Desktop\Pictures", filename2)

            IO.File.Move(filePath2, newPath2)

        Next filePath2

        MessageBox.Show("Pictures Compiled And Cleaned")
    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click

        Dim filePaths3 = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.lnk")

        For Each filePath3 In filePaths3
            Dim filename3 = IO.Path.GetFileName(filePath3)
            Dim newPath3 = IO.Path.Combine("C:\Users\bj\Desktop\Shortcuts", filename3)

            IO.File.Move(filePath3, newPath3)

        Next filePath3

        Dim filePaths6 = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.url")

        For Each filePath6 In filePaths6
            Dim filename6 = IO.Path.GetFileName(filePath6)
            Dim newPath6 = IO.Path.Combine("C:\Users\bj\Desktop\Shortcuts", filename6)

            IO.File.Move(filePath6, newPath6)

        Next filePath6

        MessageBox.Show("Shortcuts Compiled And Cleaned")
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


        Dim filePaths4 = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.mp4")

        For Each filePath4 In filePaths4
            Dim filename4 = IO.Path.GetFileName(filePath4)
            Dim newPath4 = IO.Path.Combine("C:\Users\bj\Desktop\Videos", filename4)

            IO.File.Move(filePath4, newPath4)

        Next filePath4

        Dim filePaths5 = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.avi")

        For Each filePath5 In filePaths5
            Dim filename5 = IO.Path.GetFileName(filePath5)
            Dim newPath5 = IO.Path.Combine("C:\Users\bj\Desktop\Videos", filename5)

            IO.File.Move(filePath5, newPath5)

        Next filePath5

        MessageBox.Show("Videos Compiled And Cleaned")

    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        My.Computer.FileSystem.CreateDirectory(
"C:\Users\bj\Desktop\Shortcuts")
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        My.Computer.FileSystem.CreateDirectory(
"C:\Users\bj\Desktop\Videos")
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        My.Computer.FileSystem.CreateDirectory(
"C:\Users\bj\Desktop\Pictures")
    End Sub
End Class

错误代码

IO.File.Move(filePath, newPath) 返回错误,

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim filePaths = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.png")

        For Each filePath In filePaths
            Dim filename = IO.Path.GetFileName(filePath)
            Dim newPath = IO.Path.Combine("C:\Users\bj\Desktop\Pictures", filename)

            IO.File.Move(filePath, newPath)

        Next filePath

错误信息:System.IO.IOException: 'Cannot create a file when that file already exists.

【问题讨论】:

  • edit问题并包括所有exception details。另外,请确保您的代码遵循minimal reproducible example 的准则。
  • 这是整个异常细节,我现在也展示了返回错误的部分。
  • 这不是 GetFiles 不起作用 - 所以问题标题有点不正确.. ;)
  • “除非他们以前在那个文件夹中,否则他们都不会进入那个文件夹”——你能更详细地解释一下吗?我从未听说过 Windows 拒绝将文件移动到文件夹中,除非它最近已从该文件夹中删除
  • @CaiusJard 是的,我也不知道怎么做,我设置了将所有 .lnk 文件移动到一个文件夹中,当我第一次运行该程序时,它移动了大部分 .lnk 文件,尽管确实离开了一些。当我再次运行程序时,它仍然没有移动这些文件,我将其中一个 .lnk 文件放入文件夹中,然后放回桌面,当我运行程序时,文件在我运行程序时移动。不确定这是否与文件有关,但我不得不假设。

标签: vb.net directory getfiles


【解决方案1】:

看看the documentation for File.Move - 示例代码首先检查文件是否存在并删除它

Wa 可以扩展此逻辑以提供更好的体验。

If IO.File.Exists(newPath) Then
    Dim dr = MessageBox.Show($"File {newPath} exists, do you want to keep both files? The recently moved file will have a number added to its name", "", MessageBoxButtons.YesNoCancel)

    Select dr 
      Case DialogResult.Cancel
        Continue 'go to next loop iteration

      Case DialogResult.No
        IO.File.Delete(newPath)

      Case DialogResult.Yes 'keep both

        'Make the path eg kitten.1.jpg, kitten.2.jpg until we find a free name
        Dim x = 0
        Do
          x += 1
          newPath = IO.Path.ChangeExtension(newPath, i & IO.Path.GetExtension(newPath))
        While IO.File.Exists(newPath)

    End Select

End If

【讨论】:

  • 这个位置会在IO.File.Move(filePath, newPath)Next filePath 之间吗?
  • 我可以让您多考虑一下吗?阅读文档;他们在哪里进行 File.Exists 检查 - 如果文件已经存在,则在 Move 之前或之后崩溃?逻辑在哪里指示您要在调用崩溃(如果确实存在)之前或在移动文件之后进行文件存在于新位置的检查?如果您在将文件移动到该路径后检查特定路径中是​​否存在文件,并且在那里找到文件(因为您只是将文件移动到那里)询问用户是否要删除,会发生什么情况是吗?
  • 好的,我现在已经解决了。我现在面临另外两个可能非常简单的问题,Do must end with a matching loopWhile must end with a matching End While。我知道我必须添加一个loop 和一个End While,尽管不确定它们的去向。 Visual Studio 建议了一个修复程序,将“循环”放在“Do”下,将“End While”放在“While”下,当我单击“是”以保留这两个文件时,软件会崩溃。另一个问题是“我”没有被声明,我可以将它定义为空,但我确定它有目的吗?
  • 啊,那是因为我在编写 Do 循环时犯了语法错误。它只是在while之前缺少“循环”这个词。没有这个 vb 认为应该是 Do 循环的一部分的单词“while”是一个单独的 while 循环,因此有两个错误
  • 你可以在the documentation阅读更多关于Do循环的内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多