Free language processing service and NLP C# code
Free a Project from SVN Version Control

NLP Task

Make a project no longer under SVN version control by recursively deleting the .svn Folders.

Algorithms and Implementation

Create a Console application and use the FolderBrowseDialog to let user choose the project to remove its SVN version control.

Get all of the .svn folders and remove them. The key algorithm lies in RemoveFolder. We iterates all items of a folder. For each item, if it is a file, change its Attribute to Normal and delete it; if it is a folder, we call the method itself to remove that folder. After all items of a folder have been removed, remove the empty folder in one step.

Code (Download)


using System;

 // Needed for browsing and select folder 
using System.Windows.Forms;

 // Needed for File and Directory operation 
using System.IO;

namespace RemoveVersionControl
{
    class Program
    {
        [STAThread] // This attribute is needed to show the folders. 
        static void Main( string[] args )
        {
            Console.WriteLine( "Choose the project under svn control.\n" );
            FolderBrowserDialog dialogBox = new FolderBrowserDialog( );

            if ( dialogBox.ShowDialog( ) == DialogResult.OK )
            {
                 // Select all sub folders containing ".svn". 
                 // By deleting all of those svn-folders, the project 
                 // will not be under version control any more. 
                string[] svnFolders = Directory.GetDirectories( dialogBox.SelectedPath,
                    "*.svn" , SearchOption.AllDirectories );

                 // If no sub folder contains ".svn", the project is not 
                 // under svn version control. 
                if ( svnFolders.Length == 0 )
                {
                    Console.WriteLine( "This project is not under svn control.\n" );
                    return;
                }

                 // Now delete all svn folders 
                foreach ( string svnFolder in svnFolders )
                {
                    RemoveFolder( svnFolder );
                }

                Console.WriteLine( "Version control removed successfully.\n" );
            }
        }

         // The key algorithm lies in this recursive method. 
        private static void RemoveFolder( string folderFullPath )
        {

             // Get all items contained under this folder at this level. 
             // They are files or sub-folders. 
            FileSystemInfo[] items = ( new DirectoryInfo( folderFullPath ) ).GetFileSystemInfos( );

             // Remove all contained items first 
            foreach ( FileSystemInfo item in items )
            {
                 // If an item is a file,  
                 // set its FileAttribute to Normal, then delete it immediately. 
                if ( item is FileInfo )
                {
                    File.SetAttributes( item.FullName, FileAttributes.Normal );
                    File.Delete( item.FullName );
                }
                else
                {
                     // If an item is a folder,  
                     // call this method recursively to remove this folder itself. 
                    RemoveFolder( item.FullName );
                }
            }

             // Delete the now empty folder itself. Note that a folder cannot be deleted 
             // if it contains any item. That's why the above code is needed. 
            Directory.Delete( folderFullPath );
        }
    }
}

Figures

Note

The Main method is adorned with the [STAThread] attribute to make sure the FolderBrowseDialog works properly. Try not using this attribute to see what will happen.

Of course, this code is only for the purpose of illustrating how to use the IO methods and recursion. In practice, the svn export command can check out a clean copy with no .svn admin folders at all.


Skip Navigation LinksHome > Nlp Sample Code > IO Skills > Free a Project from SVN Version Control