473,408 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 31716
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
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
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
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
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
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
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
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
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
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.