Tortoise SVN Automation Using C#

Automation of these activities will help to automate code setup or build process of an application. For automation, we need to have tortoise SVN installed along with “Command line client tools” as in the following screenshot.

Command line tools will install svn.exe, that is helpful to pull code, rename, delete, etc. using a command prompt instead of SVN GUI. Let’s create a console application in Visual Studio and name it SVNAutomation.

We will write the following method, that will run svn.exe with arguments necessary to pull code from SVN as in the following.

private static void DownloadSVNCode(string phyPath, string svnUrl, string uname, string pwd, string svnInstallPath)
{
    string arguments = "checkout " + svnUrl + " " + phyPath + " --username " + uname + " --password " + pwd;
    ProcessStartInfo info = new ProcessStartInfo("svn.exe", arguments)
    {
        WorkingDirectory = svnInstallPath,
        WindowStyle = ProcessWindowStyle.Normal
    };
    Process.Start(info);
    Console.WriteLine("Download started");
}

We are passing the command checkout to pull the code followed by SVN URL, local path, user name and password. Similarly, we can use other commands such as delete, rename for deleting and renaming items.

We will use the following code in the Main method to take input such as SVN path, local path etc. for downloading the code from SVN.

Console.Write("Please Enter SVN URL: ");
string svnPath = Console.ReadLine();

Console.Write("Please Enter SVN username: ");
string username = Console.ReadLine();

Console.Write("Please Enter SVN password: ");
string pwd = Console.ReadLine();

Console.Write("Please Enter Local path to pull code from SVN: ");
string path = Console.ReadLine();

Console.Write(@"Please Enter Installation path of SVN [Default: C:\Program Files\TortoiseSVN\bin]: ");
string installPath = Console.ReadLine();

if (!Directory.Exists(path))
{
    Directory.CreateDirectory(path);
}

if (String.IsNullOrEmpty(installPath))
{
    installPath = @"C:\Program Files\TortoiseSVN\bin";
}

DownloadSVNCode(path, svnPath, username, pwd, installPath);
Console.ReadLine();

Run the application and enter the details as in the following and then it will start pulling code from SVN in a new window.

We can extend this application further by passing various commands to svn.exe and automate the code setup or build process. I am ending the things here and attaching the source code for reference. I hope this article will be helpful for all.


Similar Articles