"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
Printable View
"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
not sure exactly what u'r after, but this is a sample on how to lock a file while working on it
VB Code:
Dim l Open "C:\test.txt" For Output Lock Read Write As #1 For l = 0 To 999999 Print #1, l Next l
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.
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
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.
Do you mean lock it even when your program isn't running? That could be more complex...
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.
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
Lock #n
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
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
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?
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 - 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.
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...
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