|
-
May 21st, 2010, 03:31 PM
#1
Thread Starter
Frenzied Member
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.
-
May 21st, 2010, 06:18 PM
#2
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
May 21st, 2010, 10:38 PM
#3
Thread Starter
Frenzied Member
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.
-
May 22nd, 2010, 04:28 AM
#4
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?
-
May 22nd, 2010, 10:54 AM
#5
Thread Starter
Frenzied Member
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.
-
May 22nd, 2010, 03:41 PM
#6
Re: How to lock file?
Yes but you only want to lock them when users are editing them via your program right?
-
May 22nd, 2010, 04:35 PM
#7
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)?
-
May 22nd, 2010, 04:41 PM
#8
Thread Starter
Frenzied Member
Re: How to lock file?
 Originally Posted by minitech
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?
-
May 22nd, 2010, 04:43 PM
#9
Thread Starter
Frenzied Member
Re: How to lock file?
 Originally Posted by chris128
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.
-
May 22nd, 2010, 04:47 PM
#10
Thread Starter
Frenzied Member
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...
-
May 22nd, 2010, 04:51 PM
#11
Thread Starter
Frenzied Member
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??
-
May 22nd, 2010, 04:54 PM
#12
Re: How to lock file?
 Originally Posted by dbassettt74
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))
-
May 22nd, 2010, 05:12 PM
#13
Thread Starter
Frenzied Member
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.
-
May 22nd, 2010, 05:16 PM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|