0

So. I have an image that can be set as the background image on my vb app and when the user wants to change the image, I have it so that it fetches their chosen image from a showdialog and puts it into a specific filepath for the rest of the program to access. But, when the process is done more than once on its initial run (meaning there is no image in the directory yet) it gives and error saying the process cannot be completed due to ("C:\userdata" & ProteusLogin.txtUsername.Text & "" & "backgroundimage.jpg", "delete.jpg") process being in use.

Here is the code.

Private Sub RDBCustom_doubleclick(sender As Object, e As EventArgs) Handles RDBCustom.Click
        RBLight.Checked = False
        RBOriginal.Checked = False
        RBDark.Checked = False
        Dim openfiledialog1 As New OpenFileDialog
        Try
            My.Computer.FileSystem.CopyFile(openfiledialog1.FileName, "C:\userdata\" & ProteusLogin.txtUsername.Text & "\" & "backgroundimage.jpg")
        Catch
            If System.IO.File.Exists("C:\userdata\" & ProteusLogin.txtUsername.Text & "\" & "backgroundimage.jpg") = True Then
                My.Computer.FileSystem.RenameFile("C:\userdata\" & ProteusLogin.txtUsername.Text & "\" & "backgroundimage.jpg", "delete.jpg")
                System.IO.File.Delete("C:\userdata\" & ProteusLogin.txtUsername.Text & "\" & "delete.jpg")
                My.Computer.FileSystem.CopyFile(openfiledialog1.FileName, "C:\userdata\" & ProteusLogin.txtUsername.Text & "\" & "backgroundimage.jpg")
            End If

        End Try
    End Sub
dippas
  • 58,591
  • 15
  • 114
  • 126
  • One thing I've done is to test if the file is ready before trying to process it. Here is a good example: https://stackoverflow.com/questions/11287502/vb-net-checking-if-a-file-is-open-before-proceeding-with-a-read-write – Pete -S- Jul 22 '20 at 22:05
  • 1
    Dispose of the current Image before trying to do anything with the file it came from. Or, (better) don't leave the Stream opened: load the Image using `File.ReadAllBytes()` (i.e., `[SomeControl].Image = Image.FromStream(new MemoryStream(File.ReadAllBytes("[Image Path]")))`) – Jimi Jul 22 '20 at 22:16
  • Have you omitted some code form that method because you are creating an `OpenFileDialog` and then using its `FileName` property without ever calling `ShowDialog`? – jmcilhinney Jul 23 '20 at 00:34
  • One very important aspect of writing code is being consistent. If you do things different ways then people reading your code will wonder why and, if you do so for no reason then you're causing confusion for no reason. There's no reason for you to be jumping back and forth between `System.IO` and `My.Computer.FileSystem` in that code. You should be using one or the other for all those operations. Also, if you are going to use types from a namespace multiple times, e.g. `System.IO`, then import the namespace once rather than writing it out in full multiple times. – jmcilhinney Jul 23 '20 at 00:41
  • Don't build file and folder paths using string concatenation and don't use the same complex expression multiple times. You use `"C:\userdata\" & ProteusLogin.txtUsername.Text & "\" & "backgroundimage.jpg"` four times when you shouldn't be using it once. Build the path properly, i.e. `Dim sourceFilePath = Path.Combine("C:\userdata", ProteusLogin.txtUsername.Text, "backgroundimage.jpg")`, and then use that variable multiple times. Note that `Path.Combine` will insert slashes as needed and will also remove slashes where they are doubled, which is one big reason to use it. – jmcilhinney Jul 23 '20 at 00:45

0 Answers0