Click to See Complete Forum and Search --> : DirectoryEntry for a single file


ireland
November 11th, 2008, 11:15 AM
How can one set security on single file?

I'm trying to set the security permissions on an individual file on IIS. I've already used DirectoryEntry and it's Properties collection to set permissions at directory level but can't get it for file only, first of all I cannot new a DirectoryEntry with the url of a file, secondly if I new up a DirectoryEntry with the directory and then look for the Children it also fails, the debugger shows me an exception is thrown when trying to access the Children collection,

string myfileurl = url + "/myfile.asmx";

//if (DirectoryEntry.Exists(myfileurl))//fails
if (DirectoryEntry.Exists(url))
{

DirectoryEntry entry = new DirectoryEntry(url);
DirectoryEntry myfilesEntry = entry.Children.Find("myfile.asmx", "");//fails, Children throws an exception
PropertyCollection props = myfilesEntry.Properties;
Int32 currentValue = (Int32)props["AuthFlags"].Value;
entry.Properties["AuthFlags"][0] = 4;
entry.CommitChanges();
}

Arjay
November 11th, 2008, 04:43 PM
What is the exception?

ireland
November 12th, 2008, 04:02 AM
the Find throws a DirectoryNotFoundException, when I look at the Children in the debugger the Count property has thrown a NotImpementedException with the message "The method or operation is not implemented.".

Could this mean that DirectoryEntry.Children.Find will only find child directories and not files?

The answer appears to be Yes.
A simple foreach shows the children as the directories only:
foreach(DirectoryEntry childEntry in entry.Children)
{
Console.WriteLine(" child: " + childEntry.Name);
}

results:
App_Code
bin

Arjay
November 12th, 2008, 09:38 AM
Take a look at the DirectoryInfo and FileInfo classes.