Showing posts with label Subversion. Show all posts
Showing posts with label Subversion. Show all posts

Monday, March 9, 2009

Deleting .svn Folders for Subversion Using C#

This past weekend I needed the ability to programmatically delete the .svn folders used by subversion in my project. I created some code in C# to change the subversion files to be writable and delete them.

Here is how to use it:
DeleteSubFolders(@"c:\svn\MyProject\Trunk",".svn",true);

Here is the code:

public static void DeleteSubFolders(string folderPath, string wildcardPattern, bool top)

{

String[] list;


//If we are at the top level, get all directory names that match the pattern

if (top)

list = Directory.GetDirectories(folderPath, wildcardPattern, SearchOption.AllDirectories);

else //Get directories and files for matching sub directories

list = Directory.GetFileSystemEntries(folderPath, wildcardPattern);


foreach (string item in list)

{

//Sub directories

if (Directory.Exists(item))

{

//Match all sub directories

DeleteSubFolders(item, "*", false);

}

else // Files in directory

{

//Get the attribute for the file

FileAttributes fileAtts = File.GetAttributes(item);

//If it is read only make it writable

if ((fileAtts & FileAttributes.ReadOnly) != 0)

{

File.SetAttributes(item, fileAtts & ~FileAttributes.ReadOnly);

}

File.Delete(item);

}

}

//Delete the matching folder that we are in

if (top == false)

{

Directory.Delete(folderPath);

}

}