Accessing "in Use" Files == ?
Results 1 to 9 of 9

Thread: Accessing "in Use" Files == ?

  1. #1
    Banned shakuni's Avatar
    Join Date
    Aug 2007
    Posts
    24

    Accessing "in Use" Files == ?

    How can I access the files that are in use by other processes using win32api ?

    Say there is this file called "locked.txt", which is in use by process A. Now how can I copy this file to another location programmatically. Normal methods will fail with the error "The process cannot access the file because it is being used by another process.".

    Any ideas ?

  2. #2
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,403
    Simplest would be to stop the process that's using it..

    There's usually a very good reason why certain files are locked exclusively by a process.
    Normally a file is only locked for writing not reading (only one process can write to it, multiple processes can read it).

    What file are we talking about?
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  3. #3
    Super Moderator: GMT Zone nihil's Avatar
    Join Date
    Jul 2003
    Location
    United Kingdom: Bridlington
    Posts
    17,190
    Hmmm,

    I recall discussing this with one of my development team a very long time ago.

    I believe that you have to use the FileShare enumerator and specify Read or ReadWrite. If I remember correctly the default is None?

    I also remember that diagrams had to be handled differently because they were image/bitmap class files. I did a quick Google and found this:

    What's clear to me is that the Image and/or Bitmap class leave a file handle open after the bitmap is loaded. Code like this will always fail with a "generic error" exception:

    Dim bmp As Image = Bitmap.FromFile("c:\temp\test2.bmp")
    bmp.Save("c:\temp\test2.bmp")

    And this code works:

    Dim bmp As Image = Bitmap.FromFile("c:\temp\test2.bmp")
    Dim cpy As New Bitmap(bmp)
    bmp.Dispose()
    cpy.Save("c:\temp\test2.bmp")

    The Dispose call is critical, that seems to actually close the file handle.
    It certainly can be done as I distinctly remember using MS Word and it would warn you if someone else had a file open for editing and actually offered to make a copy of it for you.

    As you gave a .txt file as an example I just ran a little experiment opening the same file in both notepad and wordpad. I could edit it in both at the same time...............last file saved "won"
    If you cannot do someone any good: don't do them any harm....
    As long as you did this to one of these, the least of my little ones............you did it unto Me.
    What profiteth a man if he gains the entire World at the expense of his immortal soul?

  4. #4
    Senior Member
    Join Date
    Sep 2001
    Posts
    534
    Quote Originally Posted by nihil View Post
    Hmmm,

    I recall discussing this with one of my development team a very long time ago.

    I believe that you have to use the FileShare enumerator and specify Read or ReadWrite. If I remember correctly the default is None?

    I also remember that diagrams had to be handled differently because they were image/bitmap class files. I did a quick Google and found this:



    It certainly can be done as I distinctly remember using MS Word and it would warn you if someone else had a file open for editing and actually offered to make a copy of it for you.

    As you gave a .txt file as an example I just ran a little experiment opening the same file in both notepad and wordpad. I could edit it in both at the same time...............last file saved "won"
    Well the reason why the last file that was saved, won was because the lock was placed on it and the contents were written then the lock was removed. Now since there is no call back in word pad so it was not notified of the change. And so when the word pad file was saved, it overwrote.

    I think the op is asking about accessing the file when a lock on it is present already. And by accessing I mean trying to write to it
    Last edited by intruder; April 18th, 2012 at 06:07 AM.
    A laptop, internet connection and beer.

  5. #5
    Administrator
    Join Date
    Apr 2011
    Location
    USA
    Posts
    238
    Me thinks that the OP that started this thread Four YEARS ago won't be back due to his banishment

  6. #6
    Senior Member
    Join Date
    Sep 2001
    Posts
    534
    Quote Originally Posted by Steve R Jones View Post
    Me thinks that the OP that started this thread Four YEARS ago won't be back due to his banishment
    I guess you are right but it's hard to get over the habit of responding
    A laptop, internet connection and beer.

  7. #7
    Super Moderator: GMT Zone nihil's Avatar
    Join Date
    Jul 2003
    Location
    United Kingdom: Bridlington
    Posts
    17,190
    I think that you are missing the point here?

    If we use a simple application such as notepad or wordpad and we both open the same record and make changes to it; the last person to save their record will overwrite what the other person did. Including any valid changes they might have made that were not repeated in the last record. Locking only takes place for the few milliseconds the update takes.

    In a collaborative system (such as MS office), if you open the record with edit rights you lock the record against all other potential editors. They can still read and copy the master record, but they cannot edit it, or save an edited copy.

    That way, subsequent edits will overwrite that which is actually changed and NOT revert any data, inadvertently or fraudulently.

    The OP was never clear about his circumstances, but I can think of a lot of situations where allowing a programmatic copy of a record that has been locked for editing would be a very bad idea from a security and business viewpoint. Invoices addresses, ship to addresses, credit limits, to name but a few. He did say this:

    Now how can I copy this file to another location programmatically. Normal methods will fail with the error "The process cannot access the file because it is being used by another process.".
    Last edited by nihil; April 19th, 2012 at 03:12 PM.
    If you cannot do someone any good: don't do them any harm....
    As long as you did this to one of these, the least of my little ones............you did it unto Me.
    What profiteth a man if he gains the entire World at the expense of his immortal soul?

  8. #8
    HYBR|D
    Guest
    Quote Originally Posted by Steve R Jones View Post
    Me thinks that the OP that started this thread Four YEARS ago won't be back due to his banishment
    Intruder actually replied to the topic adding some useful info to the OP question, thus i really don't have an issue with the thread being bumped.

    It's always been one of those unspoken rules here at AO.

  9. #9
    Super Moderator: GMT Zone nihil's Avatar
    Join Date
    Jul 2003
    Location
    United Kingdom: Bridlington
    Posts
    17,190
    Well, as I read it, intruder replied to my post, albeit a little belatedly

    I have no problems with that, as this is a hardy perennial subject if ever there was one. In fact, I was somewhat disappointed at the lack of response in the first place?

    This isn't just a computer efficiency issue; it has a direct impact on the security of your financial systems.

    For example, say we both grab a copy of the original record and you change the telephone number whilst I change the credit terms from $1,000,000 to cash with order.............only you submit your update after mine?

    Don't ya just love third normal form databases?
    If you cannot do someone any good: don't do them any harm....
    As long as you did this to one of these, the least of my little ones............you did it unto Me.
    What profiteth a man if he gains the entire World at the expense of his immortal soul?

Similar Threads

  1. How to Create bat files....
    By cool_boy in forum General Computer Discussions
    Replies: 3
    Last Post: June 29th, 2006, 03:45 AM
  2. Genral Windows Tips
    By Nokia in forum Tips and Tricks
    Replies: 0
    Last Post: June 12th, 2004, 06:36 PM
  3. The history of the Mac line of Operating systems
    By gore in forum Operating Systems
    Replies: 3
    Last Post: March 7th, 2004, 08:02 AM
  4. Tcp/ip
    By gore in forum Newbie Security Questions
    Replies: 11
    Last Post: December 29th, 2003, 08:01 AM
  5. Camouflage v1.2.1 The programme
    By Gbin@ryR in forum AntiOnline's General Chit Chat
    Replies: 8
    Last Post: February 28th, 2002, 05:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •