Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET File I/O and Registry > Check if folder is writeable?


Reply
 
Thread Tools Display Modes
  #1  
Old 10-25-2008, 06:28 PM
gpraceman's Avatar
gpraceman gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 556
Question Check if folder is writeable?


I want to save a bitmap object to file, but I want to make sure the file path is writeable first. The user should have write permissions to the folder and it should not be a location on a CD drive.

Right now, it gives a "A generic error occurred in GDI+", which is not too helpful. I'd rather tell the user that the selected save location is not writeable.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #2  
Old 10-26-2008, 11:09 PM
gpraceman's Avatar
gpraceman gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 556
Default

Unless someone knows a way to do this, I will just catch the System.Runtime.InteropServices.ExternalException and then let the user know the bitmap was not able to be saved. I'm not sure what other possible scenarios will generate that exception, so this may end up masking the real problem.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #3  
Old 10-26-2008, 11:20 PM
Roger_Wgnr's Avatar
Roger_Wgnr Roger_Wgnr is offline
CodeASaurus Hex

Forum Leader
* Expert *
 
Join Date: Jul 2006
Location: San Antonio TX
Posts: 2,427
Default

You should be able to check the readonly attribute of the folder.
__________________
Code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. ~Martin Golding
The user is a peripheral that types when you issue a read request. ~Peter Williams
MSDN Visual Basic .NET General FAQ
Reply With Quote
  #4  
Old 10-26-2008, 11:30 PM
gpraceman's Avatar
gpraceman gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 556
Default

I know how to check the readonly attribute for a file, but have not found a way to do that for a folder.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #5  
Old 10-27-2008, 09:58 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Quote:
Originally Posted by gpraceman View Post
I know how to check the readonly attribute for a file, but have not found a way to do that for a folder.
If this is true, then your code for checking if a file has the read-only attribute is incorrect. The only difference is replacing the word "File" with "Directory" in one of the class names:
Code:
    Private Function IsDirectoryWritable(ByVal path As String)
        Dim info As New DirectoryInfo(path)
        Return (info.Attributes And FileAttributes.ReadOnly) <> FileAttributes.ReadOnly
    End Function
I tested this on a directory on a CD-ROM drive, a writable directory on my hard drive, and a nonexistent directory and all three behaved as expected (it returns False for a directory that doesn't exist.)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 10-27-2008, 10:06 AM
gpraceman's Avatar
gpraceman gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 556
Default

Thanks, I'll give that a try.

I had been using the IsReadOnly property to determine if a file was readonly. I had been expecting that there would be a similar property for a directory, but could not find one.

Code:
Public Shared Function IsFileReadOnly(ByVal sFile As String) As Boolean
  Dim objFileInfo As FileInfo = My.Computer.FileSystem.GetFileInfo(sFile)
  Return objFileInfo.IsReadOnly
End Function
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #7  
Old 03-08-2010, 11:48 PM
gpraceman's Avatar
gpraceman gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 556
Default

Quote:
Originally Posted by AtmaWeapon View Post
If this is true, then your code for checking if a file has the read-only attribute is incorrect. The only difference is replacing the word "File" with "Directory" in one of the class names:
Code:
    Private Function IsDirectoryWritable(ByVal path As String)
        Dim info As New DirectoryInfo(path)
        Return (info.Attributes And FileAttributes.ReadOnly) <> FileAttributes.ReadOnly
    End Function
I tested this on a directory on a CD-ROM drive, a writable directory on my hard drive, and a nonexistent directory and all three behaved as expected (it returns False for a directory that doesn't exist.)
Sorry to grave dig, but I've been using similar code for awhile and never had noticed until now that the desktop will come back as being read-only. Yet, if I disable this check, my app can save a file to the desktop no problem. This looks to be an issue with other special directories, like My Documents.

What's got me scratching my head is that my app can write to some folders under My Documents, but it seems to only be the ones that my installer had created. For any other folder, the check comes back as read-only.

So, is there a reliable way to determine if a folder is read-only, even if it is within a special directory?
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->