Just another tech site

C# File Manipulation : Recursivly deleting the .svn directories C#


This example need some cleaning and exception handling of course.
but this is the idea

using System;
using System.IO;
using System.Security.AccessControl;

namespace ConsoleApplication1
{
   class Program
   {
        private static int thecounter;

        static void Main(string[] args)
        {
              thecounter = 0;
              DirectoryInfo TheRoot = new DirectoryInfo("C:/Documents and Settings/USERNAME/Mis documentos/Visual Studio 2008/Projects/ProjectA");
              LookForDir(TheRoot);

              // Write count
              Console.WriteLine("Count: {0}", thecounter);
              Console.Read();
        }

        static void LookForDir(DirectoryInfo MyRoot)
        {
              DirectoryInfo[] MySub = MyRoot.GetDirectories();
              foreach (DirectoryInfo D in MySub)
              {
                    if(D.Name.CompareTo(".svn")==0)
                    {
                           DirectorySecurity dSecurity = D.GetAccessControl();
                           dSecurity.AddAccessRule(new FileSystemAccessRule(
                                   "USERACCOUNT",FileSystemRights.DeleteSubdirectoriesAndFiles, 
                                   AccessControlType.Allow));
                           D.SetAccessControl(dSecurity);

                           Console.WriteLine(D.FullName);
                           thecounter++;
                           D.Delete(true);
                      }
                      else
                      {
                            LookForDir(D);
                      }
                  }
            }
       }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: