Connecting to SVN repository using DotSVN

← Prev   |   Next →

In this post I would like to highlight how easy it is to connect to SVN repository using DotSVN.

Steps to follow:

1) Create an instance of ISVNRepository using the SVNRepositoryFactory.

// The path to the root of an SVN FSFS repository
string repositoryPath = "file://" + "SomeValidPathToFSFS";
// Creates an ISVNRepository driver according to the protocol
// that is to be used to access a repository.
ISVNRepository repository = SVNRepositoryFactory.Create(new SVNURL(repositoryPath));
repository.OpenRepository();

2) Now that we have a connection to the repository, we can get contents using the GetDir method.

// Dictionary to receive the SVN properties
IDictionary<string, string> properties = new Dictionary<string, string>();
// Now we call GetDir to get the contents at the specified path
// Here we specified an empty string to get the contents of the root
// Second argument is the version, -1 indicated the latest version
// Third argument is the property collection
ICollection<SVNDirEntry> dirEntries = repository.GetDir("", -1, properties);

The SVNDirEntry representation of a versioned directory entry, It contains

  • The Entry name
  • Entry kind (is it a file or directory).
  • File size (in case an entry is a file)
  • The last changed revision
  • The date when the entry was last changed
  • The name of the author who last changed the entry
  • The commit log message for the last changed revision.

3) Now we can iterate through the SVNDirEntry collection.

foreach (SVNDirEntry dirEntry in dirEntries)
{
string DirName = dirEntry.Name;
System.Diagnostics.Debug.WriteLine(DirName);
}

And that is it. We can also call other methods in the repository like

// Gets the Universal Unique IDentifier (UUID) of this repository
string repostoryUUID = repository.GetRepositoryUUID(true);
// Returns the latest revision of this repository
long latestRev = repository.GetLatestRevision();