SharpSVN is a really useful library which encapsulates the functionality of the Subversion client so we can leverage it programmatically from .NET applications. I can think of many uses for this. In the past, in some of the automation frameworks I’ve worked with, updating the source code from SVN has been a manual task ran weekly. Using SharpSVN, we could write a simple console application to accomplish this. It could also be useful to automate repetitive SVN tasks in a development environment.
In this post, I’m going to outline the basics of using SharpSVN, and in the process create a simple C# console application to check-out some source code.
For starters, we’ll need to download the SharpSVN package from here. Next, create a C# console application in Visual Studio. You’ll need to target .NET 2.0.
Unzip the SharpSVN package, and add ‘SharpSVN.dll’ as a reference to your console application. Next, add the following code. We’ll pass the location we want to check-out from as an argument to the application:
string Repository = string.Empty;
if (args.Length != 1)
{
Console.WriteLine("Usage: SharpSVN
}
else
{
Repository = args[0];
}
I’m assuming here that you’re already authenticated with your SVN server. Now the code to actually perform the check-out, which is really simple:
using (SvnClient client = new SvnClient())
{
try
{
SvnUriTarget target = new SvnUriTarget(Repository);
if (client.CheckOut(target, @"C:\Working"))
{
Console.WriteLine("Successfully checked out '" + Repository + @"' to 'C:\Working'");
}
}
catch(Exception e)
{
Console.WriteLine("Error occurred during check out: " + e.ToString());
}
That’s it. The above code will check-out the source code from the SVN repository you passed as an argument to ‘C:\Working’.
The only downside I’ve seen with SharpSVN, is that it only seems to work when I target .NET 2.0. Maybe I could download the source and try to compile it under .NET 4.0, but I haven’t tried that.
I see huge usage for this library in future projects, and it’s certainly better that a previous solution I had implemented using the command line interface to the TortoiseSVN client.
Happy Coding…
