Reputation: 334
I'm trying to make a program that will check if a directory exists and if it does not then create it and move a file into it, if it does exist then don't make the directory but just move the file, although I run into an exception and I don't understand why.
Exception:
An unhandled exception of type 'System.IO.IOException' occurred in Microsoft.VisualBasic.dll
Additional information: Could not complete operation since a directory already exists in this path 'E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ModManagerModBackup'.
The code I have so far:
Private Sub Backup1_Click(sender As Object, e As EventArgs) Handles Backup1.Click
If Not My.Computer.FileSystem.DirectoryExists("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ModManagerModBackup") Then
My.Computer.FileSystem.CreateDirectory("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ModManagerModBackup")
My.Computer.FileSystem.MoveFile("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ScriptHookV.dll", ("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ModManagerModBackup"))
ElseIf My.Computer.FileSystem.DirectoryExists("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ModManagerModBackup") Then
My.Computer.FileSystem.MoveFile("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ScriptHookV.dll", ("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ModManagerModBackup"))
End If
End Sub
The exception would suggest that it won't allow the creation of the directory because the directory already exists, but even when I delete the directory before running the program it still gives me the exception even though it created the directory with no problem, it also does not highlight the line where it creates the directory but rather the line that is responsible for moving the file as the source of the exception.
Some help would be appreciated :)
Upvotes: 0
Views: 157
Reputation: 22876
Also MoveFile
will create the folder if it does not exist
Private Sub Backup1_Click(sender As Object, e As EventArgs) Handles Backup1.Click
Dim path = "E:\SteamLibrary\steamapps\common\Grand Theft Auto V\"
My.Computer.FileSystem.MoveFile(path & "ScriptHookV.dll",
path & "ModManagerModBackup\ScriptHookV.dll")
End Sub
Upvotes: 1
Reputation: 117064
Others have answered this question quite well, but I thought I'd add in an extended comment in the form of an answer.
This kind of issue can often be avoided by taking the time to write your code in a succinct way that separates the data from the code. The data in this case are all of the paths.
You could write your code like this to make it clearer and easier to see bugs:
With My.Computer.FileSystem
Dim parent = "E:\SteamLibrary\steamapps\common\Grand Theft Auto V"
Dim modBackup = .CombinePath(parent, "ModManagerModBackup")
Dim dll = "ScriptHookV.dll"
If Not .DirectoryExists(modBackup) Then
.CreateDirectory(modBackup)
End If
.MoveFile(.CombinePath(parent, dll), .CombinePath(modBackup, dll))
End With
Upvotes: 2
Reputation: 247098
Your move file code is passing the destination directory name instead of the destination file name. You could even update the logic to avoid repeated code.
Private Sub Backup1_Click(sender As Object, e As EventArgs) Handles Backup1.Click
If Not My.Computer.FileSystem.DirectoryExists("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ModManagerModBackup") Then
My.Computer.FileSystem.CreateDirectory("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ModManagerModBackup")
End If
My.Computer.FileSystem.MoveFile("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ScriptHookV.dll", ("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ModManagerModBackup\ScriptHookV.dll"))
End Sub
Upvotes: 2
Reputation: 36483
It's always best if you show the full stack trace of the error. It would then be clearer that the error occurs when you call MoveFile
.
The problem is that the 2nd parameter of MoveFile
should be the destination file path, and not the destination directory path.
Right now, the following line:
My.Computer.FileSystem.MoveFile("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ScriptHookV.dll", ("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ModManagerModBackup"))
... attempts to create a file E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ModManagerModBackup
, which is obviously invalid.
You'll need to change it to:
My.Computer.FileSystem.MoveFile("E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ScriptHookV.dll", "E:\SteamLibrary\steamapps\common\Grand Theft Auto V\ModManagerModBackup\ScriptHookV.dll")
Upvotes: 2