Results 1 to 16 of 16

Thread: how to lock a file

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Location
    israel
    Posts
    81

    how to lock a file

    "lockfile" and "lockfileex"
    i want to lock a file that i m working on and i belive that these are the api functions that i need to use but i dont know how to use them.
    i need your help !
    thank you

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    not sure exactly what u'r after, but this is a sample on how to lock a file while working on it
    VB Code:
    1. Dim l
    2.     Open "C:\test.txt" For Output Lock Read Write As #1
    3.     For l = 0 To 999999
    4.         Print #1, l
    5.     Next l
    -= a peet post =-

  3. #3

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Location
    israel
    Posts
    81
    ok that thing is not what i m looking for.
    i m looking for something that will stop other users from opening the file and i want to use lock file api.
    thank you anyways.

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Well if you open a file with Lock Read Write as peet showed you no other can open the file while this lock is in use.
    Using the LockFile API can only be achieved if you open or create the file with the CreateFile API function.
    I really can't see why that is necassary since it's so easy using the Lock keyword together with the Open statement.

    Best regards

  5. #5

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Location
    israel
    Posts
    81
    ok i wasnt clear enough .
    i need to lock an exe file so that other users wont be able to open it and write their details at the same time .
    i hope you will have some kind of an example.

  6. #6
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Do you mean lock it even when your program isn't running? That could be more complex...
    You just proved that sig advertisements work.

  7. #7
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    I'm in front of the computer.
    Posts
    270
    I could be wrong, but I think what he's getting at is that the program will be on a network, and he doesn't want two people to run it and try saving information at the same time, because they might overwrite or something.
    Alphanos

  8. #8

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Location
    israel
    Posts
    81
    thats right Alphanos
    thats exactly what i want to do .
    do you guys have any idea how to do that ?
    it will help me alot .
    10x

  9. #9
    Member TheGuru's Avatar
    Join Date
    Dec 2001
    Posts
    57
    Lock #n

  10. #10
    Member TheGuru's Avatar
    Join Date
    Dec 2001
    Posts
    57
    As long as your wanting to keeping people from saving at the same time... Flock your datafiles is all ya need to do.

    for example:

    Dim x As String
    x = "Test"
    Open ("c:\data.dat") For Output As #1
    Lock #1
    Print #1, x
    Close #1

    When you close the file.. The lock is removed

  11. #11
    Lively Member
    Join Date
    Feb 2000
    Posts
    120
    i dont want to lock a text file, i want to lock an exe file that writes data to the database until i will finish fixing the database and then allow them to use it again.
    10x
    shachar shaty

  12. #12
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    if this is an acc db, u should open the db exclusively when upgrading the db.

    thats how I do it, when upgrading a db from my program.

    Try to open the db exclusive. If ok, goahead upgrade it (while keeping it open exclusively)

    doe that make any sence?

  13. #13
    Lively Member
    Join Date
    Feb 2000
    Posts
    120
    no.
    i dont want to do it in the database because the users will work and in the end when they want to save the changes they will not be able to do that, that is not good i want them to know that now is a wrong time to work with the application.
    shachar shaty

  14. #14
    Member TheGuru's Avatar
    Join Date
    Dec 2001
    Posts
    57
    shachar - I've never heard of locking a exe file cause its really not the cleanest way to do it and could result in many problems. Best way to do it is just to lock the database before you write to it then remove the lock afterwards. Then use error handling in your application to make sure that if a database is currently locked, then keep trying till it gets it done. Basicly you just want to make sure that nobody tires to do anything to a file when somene else is writing to it. Cause when your writing, you must have exclusive access to that file...

    LockFile and LockFileEx are just for working with files that are used by programs... IE: databases, ascii files, etc but not the program itself.

  15. #15
    Member TheGuru's Avatar
    Join Date
    Dec 2001
    Posts
    57
    By the way...

    You can use the lock function I showed you with databases or text files. IE: You can lock just the records you are writing to... so it doesn't effect anything else....

    LockFileEx does basicly the samething, so I figure that is just a wrapper for it.

    IE: LockFile - Locks the whole thing
    LockFileEx - Locks only the records you are going to write to...

    The lock fucntion in visual basic can do both, so there is really no need to make a API call...

  16. #16
    Lively Member
    Join Date
    Feb 2000
    Posts
    120
    ok but i dont want to add record to the file / database i want to alter tables in the database thats why i want to freeze the users.
    10x
    shachar shaty

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