File lock

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jos Roijakkers

    #1

    File lock

    At some point in my application I load an image into a picturebox with the
    code:

    pbGraphic.Image = Image.FromFile( "picture.jp g")

    Later on in the program I want to rename the file using the code:

    pbGraphic.Image = Nothing
    My.Computer.Fil eSystem.RenameF ile("picture.jp g","newname.jpg ")

    but I get an IOException: "The process cannot access the file because it
    is being used by another process."
    Why is the original file still locked and what can I do to unlock it?

    Any help is welcome.
    Tnxs


  • Andrew Morton

    #2
    Re: File lock

    Jos Roijakkers wrote:[color=blue]
    > At some point in my application I load an image into a picturebox
    > with the code:
    >
    > pbGraphic.Image = Image.FromFile( "picture.jp g")
    >
    > Later on in the program I want to rename the file using the code:
    >
    > pbGraphic.Image = Nothing
    > My.Computer.Fil eSystem.RenameF ile("picture.jp g","newname.jpg ")
    >
    > but I get an IOException: "The process cannot access the file because
    > it is being used by another process."
    > Why is the original file still locked and what can I do to unlock it?[/color]

    You have to tell it you've really, really finished with it using
    ..Dispose():-

    pbGraphic.Image = Nothing
    pbGraphic.Dispo se()
    My.Computer.Fil eSystem.RenameF ile("picture.jp g","newname.jpg ")

    And I'm fairly sure you should be giving the full path for the original and
    new names in the RenameFile().

    Andrew


    Comment

    • Jos Roijakkers

      #3
      Re: File lock

      Unfortunately, adding Dispose() makes no difference.
      Re the filenames: the original name should be given with a full path, the
      new name should not have the full path.
      [color=blue]
      > Jos Roijakkers wrote:
      >[color=green]
      >> At some point in my application I load an image into a picturebox
      >> with the code:
      >>
      >> pbGraphic.Image = Image.FromFile( "picture.jp g")
      >>
      >> Later on in the program I want to rename the file using the code:
      >>
      >> pbGraphic.Image = Nothing
      >> My.Computer.Fil eSystem.RenameF ile("picture.jp g","newname.jpg ")
      >> but I get an IOException: "The process cannot access the file because
      >> it is being used by another process."
      >> Why is the original file still locked and what can I do to unlock it?[/color]
      > You have to tell it you've really, really finished with it using
      > .Dispose():-
      >
      > pbGraphic.Image = Nothing
      > pbGraphic.Dispo se()
      > My.Computer.Fil eSystem.RenameF ile("picture.jp g","newname.jpg ")
      > And I'm fairly sure you should be giving the full path for the
      > original and new names in the RenameFile().
      >
      > Andrew
      >[/color]


      Comment

      • Andrew Morton

        #4
        Re: File lock

        >> Jos Roijakkers wrote:[color=blue][color=green]
        >>[color=darkred]
        >>> At some point in my application I load an image into a picturebox
        >>> with the code:
        >>>
        >>> pbGraphic.Image = Image.FromFile( "picture.jp g")
        >>>
        >>> Later on in the program I want to rename the file using the code:
        >>>
        >>> pbGraphic.Image = Nothing
        >>> My.Computer.Fil eSystem.RenameF ile("picture.jp g","newname.jpg ")
        >>> but I get an IOException: "The process cannot access the file
        >>> because it is being used by another process."
        >>> Why is the original file still locked and what can I do to unlock
        >>> it?[/color]
        >> You have to tell it you've really, really finished with it using
        >> .Dispose():-
        >>
        >> pbGraphic.Image = Nothing
        >> pbGraphic.Dispo se()
        >> My.Computer.Fil eSystem.RenameF ile("picture.jp g","newname.jpg ")
        >> And I'm fairly sure you should be giving the full path for the
        >> original and new names in the RenameFile().[/color][/color]

        Jos Roijakkers wrote:[color=blue]
        > Unfortunately, adding Dispose() makes no difference.
        >[/color]

        If you use a program like Unlocker (http://ccollomb.free.fr/unlocker/) you
        can see which other process has the file open. I had a problem once with a
        source control program obtaining a handle to text files in the same
        directory as the app.
        [color=blue]
        > Re the filenames: the original name should be given with a full path,
        > the new name should not have the full path.[/color]

        I only have .NET 1.1 here, so I don't have the 2.0 docs to hand.
        What happens if you use the VB Rename function? Or System.IO.File. Move?
        (nothing different, at a guess).

        Andrew


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: File lock

          "Jos Roijakkers" <j.roijakkers@q red-it.nl> schrieb:[color=blue]
          > pbGraphic.Image = Image.FromFile( "picture.jp g")
          >
          > Later on in the program I want to rename the file using the code:
          >
          > pbGraphic.Image = Nothing
          > My.Computer.Fil eSystem.RenameF ile("picture.jp g","newname.jpg ")
          >
          > but I get an IOException: "The process cannot access the file because it
          > is being used by another process."[/color]

          <URL:http://dotnet.mvps.org/dotnet/code/graphics/#ImageNoLock>

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Jos Roijakkers

            #6
            Re: File lock

            Thanks Andrew for your help.

            The VB Rename function gives an error: "ArgumentExcept ion was not handled;
            Procedure call or argument is not valid"
            System.IO.File. Move gives the same IOException as the RenameFile function.
            The Unlocker program shows no other locks on the file than the application
            itself.

            So I guess I have to unlock the file right after loading it into the picturebox.
            But I don't have any clue on how to do that.

            Jos
            [color=blue][color=green][color=darkred]
            >>> Jos Roijakkers wrote:
            >>>
            >>>> At some point in my application I load an image into a picturebox
            >>>> with the code:
            >>>>
            >>>> pbGraphic.Image = Image.FromFile( "picture.jp g")
            >>>>
            >>>> Later on in the program I want to rename the file using the code:
            >>>>
            >>>> pbGraphic.Image = Nothing
            >>>> My.Computer.Fil eSystem.RenameF ile("picture.jp g","newname.jpg ")
            >>>> but I get an IOException: "The process cannot access the file
            >>>> because it is being used by another process."
            >>>> Why is the original file still locked and what can I do to unlock
            >>>> it?
            >>> You have to tell it you've really, really finished with it using
            >>> .Dispose():-
            >>>
            >>> pbGraphic.Image = Nothing
            >>> pbGraphic.Dispo se()
            >>> My.Computer.Fil eSystem.RenameF ile("picture.jp g","newname.jpg ")
            >>> And I'm fairly sure you should be giving the full path for the
            >>> original and new names in the RenameFile().[/color][/color]
            > Jos Roijakkers wrote:
            >[color=green]
            >> Unfortunately, adding Dispose() makes no difference.
            >>[/color]
            > If you use a program like Unlocker (http://ccollomb.free.fr/unlocker/)
            > you can see which other process has the file open. I had a problem
            > once with a source control program obtaining a handle to text files in
            > the same directory as the app.
            >[color=green]
            >> Re the filenames: the original name should be given with a full path,
            >> the new name should not have the full path.
            >>[/color]
            > I only have .NET 1.1 here, so I don't have the 2.0 docs to hand.
            > What happens if you use the VB Rename function? Or
            > System.IO.File. Move?
            > (nothing different, at a guess).
            > Andrew
            >[/color]


            Comment

            • Jos Roijakkers

              #7
              Re: File lock

              Thanks a lot. I've got it working now.
              Vielen Dank!

              Jos
              [color=blue]
              > "Jos Roijakkers" <j.roijakkers@q red-it.nl> schrieb:
              >[color=green]
              >> pbGraphic.Image = Image.FromFile( "picture.jp g")
              >>
              >> Later on in the program I want to rename the file using the code:
              >>
              >> pbGraphic.Image = Nothing
              >> My.Computer.Fil eSystem.RenameF ile("picture.jp g","newname.jpg ")
              >> but I get an IOException: "The process cannot access the file because
              >> it is being used by another process."
              >>[/color]
              > <URL:http://dotnet.mvps.org/dotnet/code/graphics/#ImageNoLock>
              >[/color]


              Comment

              Working...