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:
  1. private static void DownloadSVNCode(string phyPath, string svnUrl, string uname, string pwd,string svnInstallPath)  
  2. {  
  3.     string arguments = " checkout " + svnUrl + " " + phyPath + "  --username " + uname + " --password " + pwd;  
  4.     ProcessStartInfo info = new ProcessStartInfo("svn.exe", arguments);  
  5.     info.WorkingDirectory = svnInstallPath;  
  6.     info.WindowStyle = ProcessWindowStyle.Normal;  
  7.     Process.Start(info);  
  8.     Console.WriteLine("Download started");  
  9. }  
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:
  1. Console.Write("Please Enter SVN URL: ");  
  2. string svnPath = Console.ReadLine();  
  3.   
  4. Console.Write("Please Enter SVN username:");  
  5. string username = Console.ReadLine();  
  6. Console.Write("Please Enter SVN password:");  
  7. string pwd = Console.ReadLine();  
  8.   
  9. Console.Write("Please Enter Local path to pull code from SVN:");  
  10. string path = Console.ReadLine();  
  11.   
  12. Console.Write(@"Please Enter Installation path of SVN[Default:C:\Program Files\TortoiseSVN\bin]:");  
  13. string installPath = Console.ReadLine();  
  14. if (!Directory.Exists(path))  
  15. {  
  16.     Directory.CreateDirectory(path);  
  17. }  
  18. if (String.IsNullOrEmpty(installPath))  
  19. {  
  20.     installPath = @"C:\Program Files\TortoiseSVN\bin";  
  21. }  
  22. DownloadSVNCode(path, svnPath, username, pwd,installPath);  
  23. 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