Preventing External File Access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to prevent files from being manipulated (e.g., renamed or moved) by external applications (e.g., Windows Explorer) while my app is working with the files. The length of time that my app works with the files is determined by the user (i.e., the user manually opens and closes the files). A bonus would be to prevent external applications from opening the files, but that is not required. Would using a FileStream do what I need to do, assuming that I wouldn't call the FileStream.Close method until the user chooses to close the file? Is there a better technique?

Note that the files are typically between 2 - 10MB and they can be written and/or renamed multiple times while the user is working with them. Also, I use binary serialization to save the files.

Thanks,
Lance
 
Hi Lance,

Did you try to do that, (moving your file while it was busy (not closed) in
your program).

Normaly that should not go at all, and is it the OS that prevent this.

So I am curious how you do that, sometimes it is asked in this newsgroup how
to delete a wrong locked file?

Cor
I need to prevent files from being manipulated (e.g., renamed or moved) by
external applications (e.g., Windows Explorer) while my app is working with
the files. The length of time that my app works with the files is
determined by the user (i.e., the user manually opens and closes the files).
A bonus would be to prevent external applications from opening the files,
but that is not required. Would using a FileStream do what I need to do,
assuming that I wouldn't call the FileStream.Close method until the user
chooses to close the file? Is there a better technique?
Note that the files are typically between 2 - 10MB and they can be written
and/or renamed multiple times while the user is working with them. Also, I
use binary serialization to save the files.
 
Hi Lance,

Thanks for posting in the community.

From your description, I understand that you want to prevent a file being
shared by another process, also you wants to change the filename when you
have opened the filehandle.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I think by default the file will be open by setting the FileShare flag to
None, in this way, other process can not open the file.

Dim fs As New FileStream("c:\test.txt", FileMode.Open,
FileAccess.ReadWrite, FileShare.None)

Also when a file is opened it can not be moved or renamed. I am curious
why you need to rename a filename when it is opened. Maybe there will be a
better solution.

Please apply my suggestion above and let me know if it helps resolve your
problem.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Lance:

You could set the system.io.fileshare parameter to none...

See http://www.vbug.net/technicalsupport/topicposts.asp?CID=1&TID=243 for
more details.

HTH
Graham Parker.

Lance said:
I need to prevent files from being manipulated (e.g., renamed or moved) by
external applications (e.g., Windows Explorer) while my app is working with
the files. The length of time that my app works with the files is
determined by the user (i.e., the user manually opens and closes the files).
A bonus would be to prevent external applications from opening the files,
but that is not required. Would using a FileStream do what I need to do,
assuming that I wouldn't call the FileStream.Close method until the user
chooses to close the file? Is there a better technique?
Note that the files are typically between 2 - 10MB and they can be written
and/or renamed multiple times while the user is working with them. Also, I
use binary serialization to save the files.
 
Back
Top