"gl" <gl@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
I'm trying to delete a directory that contains readonly files. Is there
any
easy way to do this? I get a System.UnauthorizedAccessException when a
read
only file is encountered. Is there a way to make c# ignore a read only
flag,
or perhaps set the read only flag for the directory and have it apply to
all
files?
just wondering what the standard work around to this issue was.
Thanks
Use System.Management and the WMI Win32_Directory class, which has no such
restriction.
using System.management;
....
string path = @"f:test\\";
string dirObject = String.Format("win32_Directory.Name='{0}'", path);
using(ManagementObject dir = new ManagementObject(dirObject))
{
dir.Get();
ManagementBaseObject outParams = dir.InvokeMethod("Delete", null,
null);
// ReturnValue should be 0, else failure
if(Convert.ToInt32(outParams.Properties["ReturnValue"].Value) != 0)
...
}
}