C# SvnClient 获取授权用户名



我似乎找不到通过C# SvnClient获取用户名的方法。

在命令行中,您可以在命令提示符下键入"svn auth",它会在那里显示用户名。

但是,在 C# SvnClient API 中,它并不那么简单。

该 API 确实有一个"授权"属性 - 不幸的是,它没有直接的函数调用来获取用户名。

有人知道怎么做吗?

经过长时间的单独搜索,我发现可以做到。 - 虽然它不漂亮

var cachedItems = SvnClient.Authentication.GetCachedProperties(SvnAuthenticationCacheType.UserNamePassword)

不幸的是,这不会返回用户名或密码的任何公共字段。但是,这确实有一个名为"_filename"的私有字段,可用于获取用户名/密码。--- 可以使用反射访问。

foreach (var item in cachedItems)
{
///find the matching Uri to the repository you want
if (item.RealmUri.AbsoluteUri == ...)
{
var type = item.GetType();
var fields = type.GetFields();
var filename = fields.First(x => x.Name == "_filename").GetValue(item);
///Now just need to parse the file
using (var streamReader = new StreamReader(new FileStream(filename, FileMode.Open)))
{
while (streamReader.ReadLine() != "username") {}
streamReader.ReadLine(); ///There is 1 garbage line after username before the actual username
return streamReader.ReadLine();
}
}
}

最新更新