473,395 Members | 1,629 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,395 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 20 '05 #1
2 4250
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 20 '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 20 '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...
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...
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:...
10
by: GP | last post by:
Is it possible to iterate through all the controls collection and make the textboxes alone as read only.I don't see a readonly property for the Control.Can some one help me in this context? I...
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...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.