Results 1 to 10 of 10

Thread: [RESOLVED] VB 2010 Express. File lock using My.Computer.FileSystem

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2014
    Posts
    3

    Resolved [RESOLVED] VB 2010 Express. File lock using My.Computer.FileSystem

    Is it possible to code so as to lock (and later release) files so that only one user can access at a time?
    Last edited by RogerK; Feb 3rd, 2015 at 09:00 AM.

  2. #2
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: VB 2010 Express. File lock using My.Computer.FileSystem

    What kind of file?

    For text files I have used an additional gatekeeper file.

    Before a user can open the target file for read/write a routine checks the gatekeeper file.

    If the gatekeeper holds a username with datetime stamp then access to the target file is denied.
    The gatekeeper information is returned informing the user who has the file open and when.

    If the gate keeper file is empty then the new users id is stamped into it and they have control over the target file.

    This ends up being really simple system to implement.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,972

    Re: VB 2010 Express. File lock using My.Computer.FileSystem

    Couple thoughts: Define what you mean by lock. Define by "only one user". Specifically is the issue a shared resource on the network some where, or are you talking about a file on the computer? Why would multiple people access the file at the same time? Be sure to have a fail safe contingency in mind - what happens if the person that locks the file then has a crash? Or goes on vacation (I've seen it happen).

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: VB 2010 Express. File lock using My.Computer.FileSystem

    That is one of the nice things about a gatekeeper. You know who had it locked last and when.
    It is trivial then to manually or programmically clear it.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  5. #5
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: VB 2010 Express. File lock using My.Computer.FileSystem

    My experience with system outputs in large text files (.txt) is that they become locked once someone/something accesses them

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2014
    Posts
    3

    Re: VB 2010 Express. File lock using My.Computer.FileSystem

    Thanks to all. I'm talking about locking .txt files so as to limit their access to "one user only" at a time for update purposes though other users could access for enquiry-only. I like the gatekeeper-file idea but assume this means there is no suitable mechanism built in to VB. If a program reads the gatekeeper file, finds the target file is not in use then claims ownership of the target file by writing a "stamp" to the gatekeeper file there must be a chance of another would-be user reading the gatekeeper file between the read by the original program and its writing of the stamp and so also claiming ownership. Is there a way round this or do we just accept the risk?

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,972

    Re: VB 2010 Express. File lock using My.Computer.FileSystem

    You either accept the risk or you don't. I'd question if a file is even appropriate. But with out any context, it's hard to say. I'm goign to guess though that this file is on a shared folder somewhere too, and not on the local drive.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: VB 2010 Express. File lock using My.Computer.FileSystem

    Collisions depend on how many people are trying to access the gatekeeper file at any one time.
    The key is that all action on the gatekeeper is quick. Well under a second.
    You do not write your code so that it holds onto it.
    In most cases the odds of a collision are very small.

    As Techgnome suggests though there are other methods.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  9. #9
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,291

    Re: VB 2010 Express. File lock using My.Computer.FileSystem

    if one process opens a file for reading & writing, a second process (no matter if on the same pc or somewhere else on the network) cannot open the same file for read/write (will cause an exception). the second process will only be able to open the file for reading.

    regarding the gatekeeper file: you need to avoid any risk that two processes work on the same file without knowing from each other. if you dont then you create a timebomb that will cause data loss some months down the road. i really really advice that you test this locking mechanism very carefully (being burnt by this one by some code developed by externals).
    if you use a gatekeeper file then make sure you create it using FileStream Open with FileMode.CreateNew in both processes. this way it will fail if the file already exists when it tries to create it. if you use something else like a if file exists then writeall this is a timebomb again as writeall will simply overwrite the file if it was created in between the file exists check and the writeall call.

  10. #10

    Thread Starter
    New Member
    Join Date
    Sep 2014
    Posts
    3

    Re: VB 2010 Express. File lock using My.Computer.FileSystem

    Thanks. The conclusion seems to be that I must use System.IO.FileStream rather than My.Computer.FileSystem. Fair enough, though I was attracted to the latter because it seemed simpler. Moreover, I am surprised that My.Computer.FileSystem lacks a locking mechanism.

Posting Permissions

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



Click Here to Expand Forum to Full Width