umm I don't want the user to be able to move/delete a certain file when my app is running. Is there a way to "lock" a certain file without having it opened?
Printable View
umm I don't want the user to be able to move/delete a certain file when my app is running. Is there a way to "lock" a certain file without having it opened?
I don't think so not without having it opened.
in VB6 you could open it as a random file, record length 1 character and LOCK #fn,1. Anyone who tries to access the file gets an error. CAN you do something similar in VB.NET ??
Well, thats the same solution you would use in Vb.Net
http://www.vbwm.com/articles/builder...p?ArticleID=15
VB Code:
' attempts to open a file C:\TEST.TXT for read and write' access, and locks the file from other processes. Dim filename As String = "C:\TEST.TXT" Dim myFileStream As System.IO.FileStream Try ' attempt to open the file myFileStream = New System.IO.FileStream(filename, _ System.IO.FileMode.Open, _ System.IO.FileAccess.ReadWrite, _ System.IO.FileShare.None) Catch ' display an error to the user MessageBox.Show("Could not open file. Make sure " & filename & "exists.") Finally If myFileStream.CanRead Then ' read from the file here ' close the file when you are done myFileStream.Close() End If End Try
well yeah I can open the file... I just didnt know if I should leave the file open or not. Sounds like I have to leave it open in order to lock the file....
btw you used FileStream in your code and I always use StreamReader and StreamWriter. What's the big difference? :rolleyes:
from Microsoft Documentation..Quote:
Originally posted by MrPolite
well yeah I can open the file... I just didnt know if I should leave the file open or not. Sounds like I have to leave it open in order to lock the file....
btw you used FileStream in your code and I always use StreamReader and StreamWriter. What's the big difference? :rolleyes:
Quote:
StreamReader
Implements a TextReader that reads characters from a byte stream in a particular encoding
StreamWriter
Implements a TextWriter for writing characters to a stream in a particular encoding.
hmm so it sounds like that they are for text files :)Quote:
Originally posted by pirate
from Microsoft Documentation..