comii
comii

Reputation:

Protect single file!

I'm trying to protect a folder and the files inside it.
I'm able to protect the folder itself, so that if somebody clicks on it he will get a message: "You don't currently have permission to access this folder!"

But I can still access files in that folder. For example, if somebody knows the name of a file inside the folder he can type D:\ProtectedFolder\pdffile.pdf and he can open the file!

So, my question is:

Can I protect single files inside the folder?

This is the function that I use for folder lock:

   Public Function Lock(ByVal folder As
 String, ByVal user As String)
        Dim FilePath As String = folder
         Dim fs As FileSystemSecurity = File.GetAccessControl(FilePath)
         fs.AddAccessRule(New FileSystemAccessRule(user,
 FileSystemRights.ListDirectory,
 AccessControlType.Deny))
         fs.AddAccessRule(New FileSystemAccessRule(user,
 FileSystemRights.FullControl,
 AccessControlType.Deny))
         File.SetAccessControl(FilePath, fs)
       Return 0

    End Function

Thanks!

Upvotes: 1

Views: 1385

Answers (2)

user36457
user36457

Reputation:

Specify FileShare.None for File.Open. You can see my C# implementation of it here with full source code. Convert it to VB.NET if you'd like.

This is the message you receive when trying to open a file locked by the application:

alt text

I think that's what you're after.

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941257

You will also have to deny FileSystemRights.Read if you want to prevent that. And technically you have to make sure that the files inherited their rights from the folder.

Upvotes: 2

Related Questions