lilgirl Posted March 15, 2004 Posted March 15, 2004 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. Quote
*Experts* mutant Posted March 15, 2004 *Experts* Posted March 15, 2004 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 Quote
lilgirl Posted March 15, 2004 Author Posted March 15, 2004 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 Quote
*Experts* mutant Posted March 15, 2004 *Experts* Posted March 15, 2004 What you have works fine for me. What error does it show you when you say it won't let you write to the file again? Quote
lilgirl Posted March 15, 2004 Author Posted March 15, 2004 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. :( Quote
*Experts* mutant Posted March 15, 2004 *Experts* Posted March 15, 2004 Are you by any chance trying to write to a file called "file path" which I used as a place holder for a file name? :) Quote
lilgirl Posted March 15, 2004 Author Posted March 15, 2004 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: Quote
*Experts* mutant Posted March 15, 2004 *Experts* Posted March 15, 2004 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. Quote
lilgirl Posted March 15, 2004 Author Posted March 15, 2004 THANKS! you rock! i'm dumb..shoulda noticed that :) thanks again! :cool: Quote
VBUser Posted March 20, 2004 Posted March 20, 2004 Imports System.IO File.SetAttributes([Your Filename Here], FileAttributes.ReadOnly) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.