【发布时间】: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 文件放入文件夹中,然后放回桌面,当我运行程序时,文件在我运行程序时移动。不确定这是否与文件有关,但我不得不假设。