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. C#
  4. Why is the file used by another process?

Why is the file used by another process?

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 Posts 3 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.
  • L Offline
    L Offline
    Libor Tinka
    wrote on last edited by
    #1

    My app loads an image using Image.FromFile() and after some processing the image is saved in another file and the Image object is disposed. But when I need to delete original file, an exception box appears. The problem is that my app (process) is still using original image file. How to "unmark" the original file from "used" state back to normal "free to write/delete" state?

    P D 2 Replies Last reply
    0
    • L Libor Tinka

      My app loads an image using Image.FromFile() and after some processing the image is saved in another file and the Image object is disposed. But when I need to delete original file, an exception box appears. The problem is that my app (process) is still using original image file. How to "unmark" the original file from "used" state back to normal "free to write/delete" state?

      P Offline
      P Offline
      peshkunta
      wrote on last edited by
      #2

      Is your app process still running after you close/exit your app? If so, that's strange and means it was not disposed. If not, then it's most likely because your Visual Studio is open and is using it. Try running your app, with Visual Studio not running and see if that was the problem.

      1 Reply Last reply
      0
      • L Libor Tinka

        My app loads an image using Image.FromFile() and after some processing the image is saved in another file and the Image object is disposed. But when I need to delete original file, an exception box appears. The problem is that my app (process) is still using original image file. How to "unmark" the original file from "used" state back to normal "free to write/delete" state?

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        The process that is keeping the file open is yours. When you use FromFile to load an image, that file is kept open for the lifetime of the Image object (don't ask me why!) The work around is to open the file as a stream, the create the Image from that stream, then close the file.

        // The using statement will automatically Close and Dispose the
        // FileStream when you're done using it
        using(FileStream fs = new FileStream(fileName, FileMode.Open))
        {
        PictureBox1.Image = Image.FromStream(fs);
        }

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        L 1 Reply Last reply
        0
        • D Dave Kreskowiak

          The process that is keeping the file open is yours. When you use FromFile to load an image, that file is kept open for the lifetime of the Image object (don't ask me why!) The work around is to open the file as a stream, the create the Image from that stream, then close the file.

          // The using statement will automatically Close and Dispose the
          // FileStream when you're done using it
          using(FileStream fs = new FileStream(fileName, FileMode.Open))
          {
          PictureBox1.Image = Image.FromStream(fs);
          }

          RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          L Offline
          L Offline
          Libor Tinka
          wrote on last edited by
          #4

          Thanks a lot. Now it works fine, damn Image.FromFile()...

          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