Results 1 to 14 of 14

Thread: How to lock file?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    How to lock file?

    Is there a way to lock a file without having to actually open the file and maintain a "connection" to it from my VB app? This should then prevent other users from writing/deleting/renaming the file. But I also want to allow only my app to write/delete/rename if need be.

    Any help on this would be appreciated.

    Visual Studio 2010

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: How to lock file?

    Hi.
    Before someone looks in your problem.
    Is the system u use a server?Because if it is, i think it's better to use security to prevent people from using that file.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to lock file?

    It will be a "server" application. The problem is I need to allow anyone to access the file directly so security is not the way to do it. I basically need a locking mechanism that is not security related. Basically, I'm trying to create a distributed file application where Server1 and Server2 both have the same files and if User1 on Server1 open File1 for editing, it must somehow lock File1 on Server2 as well, so that User2 would not be able to edit File1 on either Server1 or Server2, until User1 unlocks File1 on Server1 and then my application would send the updated file to Server2, and unlock.

    Visual Studio 2010

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How to lock file?

    Are these files only used by your application? If so then it sounds like you might be better off just using a database rather than individual files.
    Having said that - if you only want the file to be locked whilst someone has it open for editing then why are you trying to avoid having maintaining a 'connection' to the file? Couldn't you just open and lock the file when the user opens the file for editing via your program and then just close/unlock the file when they close that part of your program?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to lock file?

    That's the key and sorry I didn't make that clear. These files are application agnostic. They are just simply file shares as visible to the user through Windows Explorer. That's why I need to somehow do this at the file level, not application level, but at the same time, I will have some sort of shell program that monitors the file share, and takes are of the updating and unlocking of the file on the other servers. But for this post, I am only interested in how to lock files at the file/OS level.

    Visual Studio 2010

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How to lock file?

    Yes but you only want to lock them when users are editing them via your program right?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: How to lock file?

    Set the Read-Only attribute and only give permission to SYSTEM to change that attribute (which you would do in your program)?

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to lock file?

    Quote Originally Posted by minitech View Post
    Set the Read-Only attribute and only give permission to SYSTEM to change that attribute (which you would do in your program)?
    I thought about the utilizing the Read-Only attribute, but wasn't sure how to accomplish this and wasn't sure if someone who has access to the file share can be prevented from modifying this attribute. So is it possible that my program (running as a service) on that machine, can modify this attribute, but regular users are prevented from doing so?

    Visual Studio 2010

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to lock file?

    Quote Originally Posted by chris128 View Post
    Yes but you only want to lock them when users are editing them via your program right?
    No. It can be any application. Notepad, Word, etc. But when that file is opened for editing, my program (running as a service on that machine) must get notified that the program is being opened, and then lock (or set read-only) the same file on all other servers. I'm wondering if it is even possible to accomplish this? Really, I'm trying to build a WAFS (Wide Area File Services) system. I wonder how existing system accomplish this? I don't need to use their application, but somehow when I open a file from a share, the WAFS service knows this and locks the same file located on other shares.

    Visual Studio 2010

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to lock file?

    I'm starting to this there must be some trickery involved in a WAFS solution. I know that I can create a shell extension program (I think that's what it's called) and install in Windows Explorer to "mimic" an actual share. So that when the user's navigate to this share, they see a list of what appear to be files, but actually, it is the shell extension (program) generating a list of files from a database, and it manages the actual serving of the files. Do you think that's what's being done? But that means installing something on everyone's desktop...

    Visual Studio 2010

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to lock file?

    Nope, I just found out that the WAFS solution that I was comparing to, does NOT install any client software. So somehow, they are able to closely monitor the file system and react to users opening files. They claim to employ "native file locking in real time". So how can I do this??

    Visual Studio 2010

  12. #12
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: How to lock file?

    Quote Originally Posted by dbassettt74 View Post
    I thought about the utilizing the Read-Only attribute, but wasn't sure how to accomplish this and wasn't sure if someone who has access to the file share can be prevented from modifying this attribute. So is it possible that my program (running as a service) on that machine, can modify this attribute, but regular users are prevented from doing so?
    Set the permissions of the file to SYSTEM - All and Everyone - Read.
    Then, you can set attributes like this:
    Code:
    Dim fi As New IO.FileInfo("location of file")
    fi.Attributes = (fi.Attributes And IO.FileAttributes.ReadOnly)
    And to remove:
    Code:
    Dim fi As New IO.FileInfo("location of file")
    fi.Attributes = (fi.Attributes And (Not IO.FileAttributes.ReadOnly))

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to lock file?

    As per my earlier note, I don't think I want to employ this method of using the read only attribute. I want to employ some way of using native file locking.

    Visual Studio 2010

  14. #14
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: How to lock file?

    That's what I'd call "native", it's completely managed by Windows.

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