Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. File in use error while renaming file

File in use error while renaming file

Scheduled Pinned Locked Moved Visual Basic
helpgraphicscomquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve Erbach
    wrote on last edited by
    #1

    Dear Group, Here's what my app is attempting: 1) rename a JPEG file temporarily 2) create a thumbnail for that JPEG 3) save the thumbnail in a separate folder 4) rename the JPEG file back to its original name 1), 2), and 3) work fine. 4) is where I have the problem. My program halts with this error: "The process cannot access the file because it is being used by another process." Here's the code:

    ' Step 1)
    My.Computer.FileSystem.RenameFile("TestImage.jpg, _
    "TestImage.jpg.working")
    ' Step 2)
    Dim bmpSS As Bitmap
    Dim intBW As Integer ' thumbnail width
    Dim intBH As Integer ' thumbnail height
    Dim bmp As Bitmap = New Bitmap("TestImage.jpg.working")
    bmpSS = New Bitmap(250, CInt(bmp.Height / (bmp.Width / 250)), _
    Imaging.PixelFormat.Format32bppArgb)
    intBW = bmpSS.Width
    intBH = bmpSS.Height
    Dim g As Graphics = Graphics.FromImage(bmpSS)
    g.DrawImage(Bmp, 0, 0, intBW, intBH)
    ' Step 3)
    bmpSS.Save("C:\Temp\TestImage_250.jpg")
    g = Nothing
    bmp = Nothing
    bmpSS = Nothing
    ' Step 4)
    My.Computer.FileSystem.RenameFile("TestImage.jpg.working", _
    "TestImage.jpg")

    As I said, it all works until it hits step 4). Is there some locking issue that I'm missing here? Regards, Steve Erbach Neenah, WI http://TheTownCrank.blogspot.com

    C 1 Reply Last reply
    0
    • S Steve Erbach

      Dear Group, Here's what my app is attempting: 1) rename a JPEG file temporarily 2) create a thumbnail for that JPEG 3) save the thumbnail in a separate folder 4) rename the JPEG file back to its original name 1), 2), and 3) work fine. 4) is where I have the problem. My program halts with this error: "The process cannot access the file because it is being used by another process." Here's the code:

      ' Step 1)
      My.Computer.FileSystem.RenameFile("TestImage.jpg, _
      "TestImage.jpg.working")
      ' Step 2)
      Dim bmpSS As Bitmap
      Dim intBW As Integer ' thumbnail width
      Dim intBH As Integer ' thumbnail height
      Dim bmp As Bitmap = New Bitmap("TestImage.jpg.working")
      bmpSS = New Bitmap(250, CInt(bmp.Height / (bmp.Width / 250)), _
      Imaging.PixelFormat.Format32bppArgb)
      intBW = bmpSS.Width
      intBH = bmpSS.Height
      Dim g As Graphics = Graphics.FromImage(bmpSS)
      g.DrawImage(Bmp, 0, 0, intBW, intBH)
      ' Step 3)
      bmpSS.Save("C:\Temp\TestImage_250.jpg")
      g = Nothing
      bmp = Nothing
      bmpSS = Nothing
      ' Step 4)
      My.Computer.FileSystem.RenameFile("TestImage.jpg.working", _
      "TestImage.jpg")

      As I said, it all works until it hits step 4). Is there some locking issue that I'm missing here? Regards, Steve Erbach Neenah, WI http://TheTownCrank.blogspot.com

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      serbach wrote:

      Dim bmp As Bitmap = New Bitmap("TestImage.jpg.working")

      You need to call Dispose on this object before you'll be able to rename or overwrite the file in question.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      S 1 Reply Last reply
      0
      • C Christian Graus

        serbach wrote:

        Dim bmp As Bitmap = New Bitmap("TestImage.jpg.working")

        You need to call Dispose on this object before you'll be able to rename or overwrite the file in question.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        S Offline
        S Offline
        Steve Erbach
        wrote on last edited by
        #3

        Christian, » You need to call Dispose on this object before you'll be able to rename or overwrite the file in question. « Thank you! That is precisely what I needed! Sincerely, Steve Erbach Neenah, WI http://TheTownCrank.blogspot.com

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups