Valvaris Posted December 30, 2009 Posted December 30, 2009 Hi @ all,its me again. This time i cant figure out why i cant get the folders to delete - and recieve a Path not found error.Situation -> There are paths to delete but sometimes the path dosent exist and the deletion process has to go an.Example -> PATH A, PATH B, PATH C ----- Some Computers have all 3 Paths but Some of them only have PATH A and C or PATH A and B or only PATH C.In CMD there is not problem - IT will Prompt for an error but goes on - in C Sharp it gets an error and Stops the Process of Deletion.ANDPATH has to remein Dynamic becouse sometimes the Folder is under C: or D: on some PCs.Here my Code:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows;using System.Windows.Forms;using System.Security.Principal;using System.Diagnostics;using Microsoft.Win32;using System.Threading;using System.IO; private void button4_Click(object sender, EventArgs e) { string f1 = Directory.GetCurrentDirectory() + @"\TEST\TEST2\TEST3"; Directory.Delete(f1); //Weiter Thread.Sleep(2000); string f2 = Directory.GetCurrentDirectory() + @"\TEST8\TEST4"; Directory.Delete(f2); //Weiter Thread.Sleep(2000); string f3 = Directory.GetCurrentDirectory() + @"\AA\BB"; Directory.Delete(f3); //Weiter Thread.Sleep(2000); string f4 = Directory.GetCurrentDirectory() + @"\DD\EE\FF"; Directory.Delete(f4); //Weiter Thread.Sleep(2000); string f5 = Directory.GetCurrentDirectory() + @"\More\to\Test"; Directory.Delete(f5); //Weiter Thread.Sleep(2000); string f6 = Directory.GetCurrentDirectory() + @"\its\fun"; Directory.Delete(f6); //Weiter Thread.Sleep(2000); string f7 = Directory.GetCurrentDirectory() + @"\more"; Directory.Delete(f7); //Weiter Thread.Sleep(2000); string f8 = Directory.GetCurrentDirectory() + @"\done\testing"; Directory.Delete(f8); //ENDE}Thanks allot in advance. Best regards Val.
gunsmokingman Posted December 31, 2009 Posted December 31, 2009 Since I dont know C Sharp but would assume they have something like this. If SomeThingExists Then Do Your Delete End If
Yzöwl Posted December 31, 2009 Posted December 31, 2009 Can you not use a basic:if (Directory.Exists (f#))
CoffeeFiend Posted December 31, 2009 Posted December 31, 2009 Like it was said before, you do want to check if it exists first. Throwing an exception is expected behavior when you try to delete something that doesn't exist.You may still want to add some error handling, particularly to handle the DirectoryNotFoundException exception (it might still happen) as well as IOException (will be thrown if the directories aren't empty and various other reasons) and possibly also UnauthorizedAccessException (thrown if you don't have sufficient permissions to delete it e.g. NTFS ACLs preventing it)As for deleting directories that may not be empty, the Delete method is overloaded, and it's probably better to use the other overload (the one with a bool), and you don't want to repeat too much of the code all over again, so you'd want to create a function similar to this (I've included XML comments, which is something else you should look into -- look at what intellisense pops up when you write "NukeDir(" somewhere):/// <summary>/// Deletes a directory if it exists, and handles the related exceptions/// </summary>/// <param name="dir">the directory name to delete</param>private void NukeDir(string dir){ if (Directory.Exists(dir)) { try { Directory.Delete(dir, true); } catch (DirectoryNotFoundException) { //handle the error whichever way you want } catch (IOException) { //handle the error whichever way you want } catch (UnauthorizedAccessException) { //handle the error whichever way you want } }}and simply call it any way you like in your button click event handler e.g.:string basePath = Environment.CurrentDirectory;string dirToDelete = basePath + @"\abc\def";NukeDir(dirToDelete);NukeDir(basePath + @"\something\else");Also, it looks like there's a LOT of namespaces at the top you don't need (unless there is more code you didn't show us). I don't see the point of the Thread.Sleep either (if you included them for debugging, why not step through the code manually instead?)
Valvaris Posted December 31, 2009 Author Posted December 31, 2009 Hi,wow thanks will give it a try at Monday next week. andthere is more code but it has nothing to do with this and is sepperated by other events ------------Thank you for the advice to write the comments in xml will look more in to this --------------Best regards Val.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now