原文地址:https://jianyun.org/archives/1090.html
SharpSvn是一个第三方的Subversion客户端.Net API库。对程序来说,使用命令行调用SVN.exe交互性不太好,不方便后续数据加工操作,还是API形式来的更方便。
SharpSvn项目主页:https://sharpsvn.open.collab.net/
使用SharpSvn唯一的需求是需要安装 Microsoft Visual C++ Runtime (The Visual C++ 2008 runtime for the .Net 2.0 compatible versions. The Visual C++ 2010 runtime for the newer .Net 4.0+ specific binaries)。
下载对于版本的库之后,项目添加SharpSvn.dll之后就可以使用相应的API了。
示例一:获取最新版本号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 public static long GetLatestRevision (string url ){ using (SvnClient client = GetSvnClient()) { SvnInfoEventArgs svnInfo; if (client.GetInfo(new SvnUriTarget(url), out svnInfo)) { return svnInfo.LastChangeRevision; } return 0 ; } } private static SvnClient GetSvnClient (){ SvnClient client = new SvnClient(); client.Authentication.Clear(); client.Authentication.UserNamePasswordHandlers += Authentication_UserNamePasswordHandlers; client.Authentication.SslServerTrustHandlers += Authentication_SslServerTrustHandlers; return client; } private static void Authentication_UserNamePasswordHandlers (object sender, sharpSvn.Security.SvnUserNamePasswordEventArgs e ){ e.UserName = "svnuser" ; e.Password = "password" ; } private static void Authentication_SslServerTrustHandlers (object sender, sharpSvn.Security.SvnSslServerTrustEventArgs e ){ e.AcceptedFailures = e.Failures; e.Save = true ; }
示例二:获取提交记录,可以指定版本范围
1 2 3 4 5 6 7 8 9 10 using (SvnClient client = GetSvnClient()){ Collection<SvnLogEventArgs> logs; if (client.GetLog(new Uri(url), new SvnLogArgs(new SvnRevisionRange(startRevision, endRevision)), out logs)) { } return 0 ; }
示例三:导出指定版本的文件到本地
1 2 3 4 5 6 7 8 9 10 11 12 13 public static bool ExportFile (string url, string filePath, long revision, string exportPath ){ if (!Directory.Exists(exportPath)) { Directory.CreateDirectory(exportPath); } exportPath = Path.Combine(exportPath, Path.GetFileName(filePath)); using (SvnClient client = GetSvnClient()) { return client.Export(new SvnUriTarget(Path.Combine(url, filePath), revision), exportPath); } }