473,385 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ReadOnly folder?

I'm trying to write a function that determines whether a directory can be written to, short of trying to create a file and catching the exception, is there any fancier way of doing it?

I tried having a look at the FileIOPermission class in System.Security.Permissions, but it didn't seem to do it....am I doing it wrong? Show me!

static bool WritableDirectory(string dir)
{
if(!Directory.Exists(dir)) return false;
try
{
FileIOPermission fp = new FileIOPermission(
FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, dir);
return ((fp.AllFiles & FileIOPermissionAccess.Write) != 0);
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
return false;
}

}
Cheers!
Nov 16 '05 #1
2 1698
Patty,

Use the DirectoryInfo class, instantiating an instance for the directory
in question. Then check to see if the Attributes property on the instance
has the value of FileAttributes.ReadOnly in it. If it does, then it is read
only, otherwise, it is not.

Of course, even if the directory is not read only, you might not have
access to it from your code. In this case, you would create an instance of
the FileIOPermissions class, with the directory and the access level you
want. Then, call Demand. If a SecurityException is thrown, then you know
you won't be able to write to it.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
I'm trying to write a function that determines whether a directory can be written to, short of trying to create a file and catching the exception, is
there any fancier way of doing it?
I tried having a look at the FileIOPermission class in System.Security.Permissions, but it didn't seem to do it....am I doing it
wrong? Show me!
static bool WritableDirectory(string dir)
{
if(!Directory.Exists(dir)) return false;
try
{
FileIOPermission fp = new FileIOPermission(
FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, dir);
return ((fp.AllFiles & FileIOPermissionAccess.Write) != 0);
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
return false;
}

}
Cheers!

Nov 16 '05 #2
DirectoryInfo dir = new DirectoryInfo("c:\\");

if ((dir.Attributes & FileAttributes.ReadOnly) > 0)
MessageBox.Show("ReadOnly!");

"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
I'm trying to write a function that determines whether a directory can be written to, short of trying to create a file and catching the exception, is
there any fancier way of doing it?
I tried having a look at the FileIOPermission class in System.Security.Permissions, but it didn't seem to do it....am I doing it
wrong? Show me!
static bool WritableDirectory(string dir)
{
if(!Directory.Exists(dir)) return false;
try
{
FileIOPermission fp = new FileIOPermission(
FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, dir);
return ((fp.AllFiles & FileIOPermissionAccess.Write) != 0);
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
return false;
}

}
Cheers!

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Darshak Shah | last post by:
Hi, * Question background : My DB is in ArchiveLog mode. I have TS in ReadOnly mode. As i know, - Oracle allows to DROP table resides in ReadOnly TS (& other objects also) even though that...
4
by: harry | last post by:
<input type="text" name="actreqto" maxlength="20" value="" onkeypress="changedDetails();" onchange="changedDetails();" readonly="readonly"> Any ideas why doesn't this work? ...
3
by: Matt | last post by:
I want to know if readOnly attribute doesn't work for drop down list? If I try disabled attribute, it works fine for drop down list. When I try text box, it works fine for both disabled and...
3
by: Michael SL | last post by:
I have a text area in which I have a client side javascript to process a "onclick". Because it is client side, I used a HtmlTextArea <TextArea id="Summary" onmouseup="SumMouseUp()" style="WIDTH:...
7
by: DareDevil | last post by:
I have written a method that should modify the folder path passed to it into one that exists and is selected by the user. It then returns a boolean depending on whether a folder path was selected by...
2
by: Patty O'Dors | last post by:
I'm trying to write a function that determines whether a directory can be written to, short of trying to create a file and catching the exception, is there any fancier way of doing it? I tried...
10
by: sunil | last post by:
Hello, I am new to c# . I have some basic programming doubts. Please help me in clarifying these doubts. I want to initialize a static and readonly field with a value returned by a static...
10
by: DragonLord | last post by:
I am creating a folder from my application and attempting to write from the filestream to the folder, however when I do I get an access denied exception. I have tried adding security rights to the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.