473,761 Members | 6,001 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.Unauthor izedAccessExcep tion 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 31743
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.Unauthor izedAccessExcep tion 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.Unauthor izedAccessExcep tion 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******** *************** ***********@mic rosoft.com...
I'm trying to delete a directory that contains readonly files. Is there
any
easy way to do this? I get a System.Unauthor izedAccessExcep tion 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.Manageme nt and the WMI Win32_Directory class, which has no such
restriction.

using System.manageme nt;
....

string path = @"f:test\\";
string dirObject = String.Format(" win32_Directory .Name='{0}'", path);
using(Managemen tObject dir = new ManagementObjec t(dirObject))
{
dir.Get();
ManagementBaseO bject outParams = dir.InvokeMetho d("Delete", null,
null);
// ReturnValue should be 0, else failure
if(Convert.ToIn t32(outParams.P roperties["ReturnValu e"].Value) != 0)
...

}
}

Nov 17 '05 #4
gl
I get a management exception error on the line " ManagementBaseO bject
outParams = dir.InvokeMetho d("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******** *************** ***********@mic rosoft.com...
I'm trying to delete a directory that contains readonly files. Is there
any
easy way to do this? I get a System.Unauthor izedAccessExcep tion 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.Manageme nt and the WMI Win32_Directory class, which has no such
restriction.

using System.manageme nt;
....

string path = @"f:test\\";
string dirObject = String.Format(" win32_Directory .Name='{0}'", path);
using(Managemen tObject dir = new ManagementObjec t(dirObject))
{
dir.Get();
ManagementBaseO bject outParams = dir.InvokeMetho d("Delete", null,
null);
// ReturnValue should be 0, else failure
if(Convert.ToIn t32(outParams.P roperties["ReturnValu e"].Value) != 0)
...

}
}

Nov 17 '05 #5

"gl" <gl@discussions .microsoft.com> wrote in message
news:3C******** *************** ***********@mic rosoft.com...
I get a management exception error on the line " ManagementBaseO bject
outParams = dir.InvokeMetho d("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******** *************** ***********@mic rosoft.com...
I get a management exception error on the line " ManagementBaseO bject
outParams = dir.InvokeMetho d("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
15656
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. *FILE29*.TXT). I'm looking for a way to do this in either Visual Basic or C(++). Thanks!
2
11155
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 nature in the docs, aside from creating an array with 'scandir' (http://publib.boulder.ibm.com/doc_link/en_US/a_doc_lib/libs/basetrf2/scandir.htm#A12F0c9) which will also give me the number of enties in that array. This seems a little overkill. I'm...
0
1440
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 delete files and folders from a shared folder (accessed by its UNC-notation). That's all ! Some path names can be long (> 200 characters ) and there are some files and folders that can be read-only. The exceptions I get:
11
5810
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 method is to be inherited by all listing classes to allow for * list-wide deletion of latent server-created ZIP files for security purposes *
1
1695
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 the two lines of code I have. First line works fine. Second line thriows an IO exception. None of the files are read only in the directory, and the directory exists. The odd thing is the files in the directory are getting deleted, but the directory...
24
7561
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 the folder but I get the error message when the procedure attempts to delete the folder selected in the BrowseforFolderbyPath function. How can I get it to delete the folder that the user has selected in the Browse for Folder dialog?
3
10301
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 on my local IIS 5.1 server (on WinXP SP2), records get deleted without any problems but when I upload the same file (which has the above DELETE query) to the actual remote server, then
0
2953
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 System.IO.DirectoryNotFoundException: Could not find a part of the path. Then it gives me a random file name from the UserProfile\Local Settings\Temporary Internet Files\Content.IE5\SomeRandomFolder. This is absolutley driving me nuts. So I am gonn give you a basic...
1
3144
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 their paired "foreign" resx for that language only and installs those resource files in a subdirectory somewhere. I'm trying to create a setup project in visual studio 2008. Ive got my Application Folder set up with my executable. Im now...
0
9531
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6609
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.