|
-
Dec 11th, 2001, 01:03 PM
#1
Thread Starter
Registered User
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
-
Dec 11th, 2001, 01:14 PM
#2
-= B u g S l a y e r =-
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
-
Dec 11th, 2001, 01:42 PM
#3
Thread Starter
Registered User
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.
-
Dec 11th, 2001, 02:03 PM
#4
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
-
Dec 16th, 2001, 02:01 PM
#5
Thread Starter
Registered User
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.
-
Dec 16th, 2001, 02:09 PM
#6
Frenzied Member
Do you mean lock it even when your program isn't running? That could be more complex...
You just proved that sig advertisements work.
-
Dec 16th, 2001, 02:35 PM
#7
Hyperactive Member
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.
-
Dec 29th, 2001, 06:43 AM
#8
Thread Starter
Registered User
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
-
Dec 29th, 2001, 07:04 AM
#9
Member
-
Dec 29th, 2001, 07:09 AM
#10
Member
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
-
Jan 2nd, 2002, 08:59 AM
#11
Lively Member
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
-
Jan 2nd, 2002, 09:19 AM
#12
-= B u g S l a y e r =-
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?
-
Jan 2nd, 2002, 09:40 AM
#13
Lively Member
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.
-
Jan 2nd, 2002, 12:11 PM
#14
Member
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.
-
Jan 2nd, 2002, 12:15 PM
#15
Member
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...
-
Jan 3rd, 2002, 03:09 AM
#16
Lively Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|