Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

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

 

My code is as follows:

 

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.

  • *Experts*
Posted

To set the file to readonly you don't need that.

The FileInfo class will provide you with Attributes property, which you can use to set file attributes.

 Dim fi As New IO.FileInfo("file path")
 fi.Attributes = fi.Attributes Or IO.FileAttributes.ReadOnly
 

Posted

hey thanks! that worked nice. one thing, how do i set it to read and write access again after that? I tried:

fi.attributes=io.fileattributes.normal
msgbox("unlocked")

 

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

 

Thanks for your help!

 

 

 

To set the file to readonly you don't need that.

The FileInfo class will provide you with Attributes property, which you can use to set file attributes.

 Dim fi As New IO.FileInfo("file path")
 fi.Attributes = fi.Attributes Or IO.FileAttributes.ReadOnly
 

Posted

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.

 

:(

Posted

lol..no no..i just wrote "file path" so that i don't have to write out the actual path.

 

weird why it wont work for me - this is what im doing:

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:

  • *Experts*
Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...