By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

c# delete directory containing read only files

P: n/a
gl
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
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
gl wrote:
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/de...butestopic.asp

--
Texeme Textcasting Technology
http://texeme.com
Nov 17 '05 #2

P: n/a
gl

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?
"John Bailo" wrote:
gl wrote:
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/de...butestopic.asp

--
Texeme Textcasting Technology
http://texeme.com

Nov 17 '05 #3

P: n/a

"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)
...

}
}

Nov 17 '05 #4

P: n/a
gl
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 [MVP]" wrote:

"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)
...

}
}

Nov 17 '05 #5

P: n/a

"gl" <gl@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
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?


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.
Nov 17 '05 #6

P: n/a
gl
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.

"Willy Denoyette [MVP]" wrote:

"gl" <gl@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
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?


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.

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.