c# delete directory containing read only files

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
gl said:
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 the File class to set the Read attribute?

http://msdn.microsoft.com/library/d.../frlrfsystemiofileclasssetattributestopic.asp
 
So the only way is to loop through all files in the directory and set the
readonly attribuite to read? That's fine. Is there a way to set a
directory/folder's read only attribute?
 
gl said:
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)
...

}
}
 
I get a management exception error on the line " ManagementBaseObject
outParams = dir.InvokeMethod("Delete", null,
null);" The directory appears to be showing correctly. Does this method work with mapped drives?

Willy Denoyette said:
gl said:
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)
...

}
}
 
gl said:
I get a management exception error on the line " ManagementBaseObject
outParams = dir.InvokeMethod("Delete", null,

Yes, it does work for mapped drives assumed you have rights to delete the
folder. How does your path string looks like and what exception do you get?

Willy.
 
I actually used an earlier option in the thread (just making each file in the
target directory read only). Thanks for your help though. I might try to get
the code working in the future and maybe post another thread if i get stuck.

Thanks.
 
Back
Top