473,503 Members | 4,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c# delete directory containing read only files

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
6 31722
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
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

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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
15623
by: Ben | last post by:
Greetings, I am looking for a way to search for and delete files based on a pattern mask. For example, the search method would find all files matching a certain pattern containing wildcards (e.g....
2
11141
by: 73blazer | last post by:
Hello, I'm writing some C++ code, and I need to be able to find the number of files in a given directory. Is it possible under AIX4.3.3 with C++ 3.6.4? I cannot seem to locate anything of this...
0
1421
by: Arjan van den Noort | last post by:
How to delete a directory (with all containing directories and files) with vb.net if the directory contains long paths ? (just like the dos command: RD /S/Q path) What I want is a routine to...
11
5756
by: comp.lang.php | last post by:
Once again, I thought my class method deleteZip() would do the trick, but it never deletes any .zip* file found in a directory: /** * Delete any latent ZIP files found in this album. This...
1
1677
by: jlskutt | last post by:
I'm getting an odd Exception thrown using Directory.Delete. Directory.Delete(publicData + "Chatrooms/K" + kingd, true); Directory.Delete(publicData + "Kingdoms/" + kingd, true); There are...
24
7511
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in...
3
10263
by: rn5a | last post by:
I am trying to delete records from a MS-Access DB table using the following query: strSQL=DELETE FROM MyTable WHERE =CInt(Request.QueryString("delete")) con.Execute strSQL When I run the app...
0
2926
by: wolfsbane | last post by:
Alright, here it is I am trying to write a win32 app in VB 2005 to clean up user's profiles. everything works correctly except for the Delete("directory", True) statement. I get a...
1
3132
by: Claire | last post by:
Ive written a small string resource building utility that I send out to our translators. I have a setup project for each language we support, which picks out a group of 12 english resx files plus...
0
7207
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
7093
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...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.