利用SharpSvn操作SVN
一、下载SharpSvn
https://sharpsvn.open.collab.net/
目标框架
.NET Framework 2.0
测试代码
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using SharpSvn;
using SharpSvn.Remote;
using SharpSvn.Security;
namespace SharpSVNTest
{
class Program
{
static void Main(string[] args)
{
SVN.Checkout();
SVN.Update();
SVN.Commit();
SVN.GetStatus();
SVN.TraversalSvnRemoteStat();
Console.WriteLine("按任意键退出...");
Console.Read();
}
}
public class SVN
{
public static string path = @"https://zwwx-svn/svn/sanguo/branches/res/2015-12-21_res_korea/android_res/resource";
public static void Checkout()
{
using (SvnClient client = new SvnClient())
{
client.CheckOut(new Uri(path),
"c:\\sharpsvn");
Console.WriteLine("Checkout Finish!");
}
}
public static void Update()
{
using (SvnClient client = new SvnClient())
{
client.Update("c:\\sharpsvn");
Console.WriteLine("Update Finish!");
}
}
public static void Commit()
{
using (SvnClient client = new SvnClient())
{
SvnCommitArgs ca = new SvnCommitArgs();
ca.LogMessage = "没做任何修改......";
client.Commit("c:\\sharpsvn", ca);
Console.WriteLine("Commit Finish!");
}
}
public static void GetStatus()
{
using (SvnClient client = new SvnClient())
{
SvnStatusArgs args = new SvnStatusArgs();
args.Depth = SvnDepth.Empty;
args.RetrieveRemoteStatus = false;
Collection<SvnStatusEventArgs> list = new Collection<SvnStatusEventArgs>();
client.GetStatus(@"c:\\sharpsvn", args, out list);
foreach (SvnStatusEventArgs eventArgs in list)
{
//从eventArgs中获取每个变更文件的相关信息
Console.WriteLine("Path={0}, Revision={1}", eventArgs.Path, eventArgs.Revision);
}
}
}
//遍历远程svn目录下的文件(非递归)
public static void TraversalSvnRemoteStat()
{
SvnRemoteSession remoteSession = new SvnRemoteSession(new Uri(path));
long rev;
remoteSession.GetLatestRevision(out rev);
Console.WriteLine("LatestRevision=" + rev);
remoteSession.List(null,
new EventHandler<SvnRemoteListEventArgs>(
delegate(object s, SvnRemoteListEventArgs e)
{
SvnRemoteSession remote = new SvnRemoteSession(new Uri(path + "/" + e.Name));
SvnRemoteStatEventArgs stat;
remote.GetStat("", out stat);
Console.WriteLine("Name={0}, Revision={1}, FileSize={2}", e.Name, stat.Entry.Revision, stat.Entry.FileSize);
}));
Console.WriteLine("遍历完成!");
}
}
}
运行效果