SVN API with C# - Browse Files in SVN

This article explains the use of the SVN API with C# to browse files.


  • Tortoise SVN
  • Browsing Local Folders and Files
  • SVN Browsing Using a Command Prompt
  • SVN Browsing Using C#
  • Note
  • References

Tortoise SVN

Tortoise SVN is a free open-source Windows client for the Apache Subversion version control system. Tortoise SVN is a central repository that manages files and directories over time. Files and directories are stored in a hierarchical structure in SVN. Here, we can upload and manage files in an effective manner. If a change is made to an uploaded file, SVN is smart enough to recover an older version of the file, examine the history of how and when your data has changed and who changed it.

Browsing Local Folders and Files

If we want to browse files locally we will use the sealed classes in C# “FolderBrowsingDialog” and “OpenFileDialog”. After this level we cannot inherit these classes since they are sealed classes. So by using the preceding classes from C#, it will open up the file and folder dialog as in Figures 2.1 and 2.2.

                                       Figure 1.1

select local folder
                                 Figure 2.1

                                                                        Figure 2.2

There are the following two ways to browse remote files in SVN:

  1. Generally users will checkout the file from SVN (make it a local copy) and use the preceding C# classes to read files and access folders.
  2. The second method is to open the SVN repository directly from the .NET application and select the files or folders.

    Again this can be done in one of the following two ways:

    • using Command Prompt
    • using C#

SVN Browsing using Command Prompt

Generally Tortoise SVN provides the commands we can execute in a command prompt interface. You can find all the commands in the Tortoise SVN Help document.

Open the command prompt, change the working directory path to “C:\Program Files\TortoiseSVN\bin” (the installation path of Tortoise SVN).

The following is the command to open the SVN repo browser:

TortoiseProc.exe /command:repobrowser /path:”SVNPATH” /outfile:” path\to\file”

In the preceding command, we have the following three parameters:

  • /command:repobrowser: Starts the repository browser dialog, pointing to the URL of the working copy given in /path or /path that points directly to an URL.

  • /path:”SVNPATH”: Here we should provide the SVN path to be opened. The path should be enclosed in double quotes and there is no space between the /path: and “SVNPATH”.

  • /outfile:”path\to\file”: Once the repo browser is opened, we will be selecting the file or folder from the browser. After selecting the file, the selected URL and revision are written to that file when the repository browser is closed. The first line in that text file contains the URL and the second line the revision in text format. The path should be enclosed in double quotes and there is no space between the /outfile: and ”path\to\file”.

So finally the selected file path from the SVN repo browser is stored in the output file path.

SVN Browsing using C#

Now, we have the command to be executed from the command prompt. The same command can also be executed from .NET using the following code.

  1. private void ExecuteSVNCommand(string SVNCommand)  
  2. {  
  3.     try  
  4.     {  
  5.         ProcessStartInfo processStartInfo = new ProcessStartInfo();  
  6.         processStartInfo.WorkingDirectory = @"C:\Program Files\TortoiseSVN\bin";  
  7.         processStartInfo.RedirectStandardInput = true;  
  8.         processStartInfo.CreateNoWindow = true;  
  9.         processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;  
  10.         processStartInfo.FileName = "cmd.exe";                  
  11.         processStartInfo.UseShellExecute = false;  
  12.         processStartInfo.Arguments = SVNCommand;  
  13.         Process process = Process.Start(processStartInfo);  
  14.         process.WaitForExit();  
  16.         int eCode = proc.ExitCode;  
  17.         if (eCode == 0)  
  18.         {  
  19.             //Success  
  20.         }  
  21.         else  
  22.         {  
  23.             //Failed  
  24.         }  
  25.     }  
  26.     catch (Exception ex)  
  27.     {  
  28.        //Catch Exception  
  29.     }  
  30. }  

If we pass the preceding command to this method, it will exit with the exit code of 1 and it will throw an exception of type “System.InvalidOperationException”.

So the command to be passed is:
  1. string command = @"/c " + "TortoiseProc.exe /command:repobrowser /path:" + SVNURL + " /outfile:" + OUTPUT_PATH;  
Here “/c” tells cmd that we want it to execute the command that follows and then terminate.

I have used the OUTPUT_PATH file path extension as .xls, so that the first cell will hold the selected URL of SVN and the second cell will hold the revision in text format. You can provide “.txt” as well.