File Permissions

lilgirl

Member
Joined
Jun 21, 2003
Messages
24
Hi,

I am trying to make a file read only in VB.

My code is as follows:

Code:
 Dim f2 As New System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.NoAccess, filen)
 MsgBox("locked")

Once the message box appears and I open the file, it allows me to write to it. What am I missing? Any thoughts are appreciated.

Thanks.
 
To set the file to readonly you dont need that.
The FileInfo class will provide you with Attributes property, which you can use to set file attributes.
Code:
  Dim fi As New IO.FileInfo("file path")
  fi.Attributes = fi.Attributes Or IO.FileAttributes.ReadOnly
 
hey thanks! that worked nice. one thing, how do i set it to read and write access again after that? I tried:
Code:
fi.attributes=io.fileattributes.normal
msgbox("unlocked")

But when I try to write to the file after the msgbox comes up, it wont let me.

Thanks for your help!



mutant said:
To set the file to readonly you dont need that.
The FileInfo class will provide you with Attributes property, which you can use to set file attributes.
Code:
  Dim fi As New IO.FileInfo("file path")
  fi.Attributes = fi.Attributes Or IO.FileAttributes.ReadOnly
 
It gives me the same message that I get when the file is set to read only:

CANNOT CREATE THE "FILE PATH" FILE.
MAKE SURE THAT THE PATH AND FILENAME ARE CORRECT.

:(
 
lol..no no..i just wrote "file path" so that i dont have to write out the actual path.

weird why it wont work for me - this is what im doing:
Code:
Dim fi As New IO.FileInfo(filen)
fi.Attributes = fi.Attributes Or IO.FileAttributes.ReadOnly
MsgBox("locked")
Dim fileS As New System.IO.StreamReader(filen)
searchMe = fileS.ReadToEnd()
fileS.Close()
fi.Attributes = fi.Attributes Or IO.FileAttributes.Normal
MsgBox("unlocked")
I get the same error message when I try to write to it after it is locked and unlocked... :confused:
 
Set the attributes to normal only like you showed in the previous post. Normal can only be used byitself so you are not really changing anything and the file is still readonly.
 
Back
Top