C#控制管理VisualSVN Server,
VisualSVN Server可以用WMI接口管理(Windows Management Instrumentation)。 VisualSVN Server安裝的計算機中,位於%VISUALSVN_SERVER%\WMI下的MOF文件描述了該接口。可以用C#代碼控制管理VisualSVN Server。
我機器VisualSVN安裝在D盤,MOF文件存在位置:D:\Program Files\VisualSVN Server\WMI\VisualSVNServer.mof
下面一些代碼供參考:
[csharp] view plaincopyprint?
- using System.Management;
- using System.Management.Instrumentation;
並添加 System.Management,System.Management.Instrumentation這兩個組件的引用。
1)創建Subversion 用戶 'user1',密碼為'secret'
[csharp] view plaincopyprint?
- ManagementClass userClass = newManagementClass("root\\VisualSVN","VisualSVN_User",null);
- // Obtain in-parameters for the method
- ManagementBaseObject inParams =
- userClass.GetMethodParameters("Create");
- // Add the input parameters.
- inParams["Name"] = "user1";
- inParams["Password"] = "secret";
- // Execute the method and obtain the return values.
- ManagementBaseObject outParams =
- userClass.InvokeMethod("Create", inParams, null);
2)創建新的repository 'Repo1'
[csharp] view plaincopyprint?
- ManagementClass repoClass = newManagementClass("root\\VisualSVN","VisualSVN_Repository",null);
- // Obtain in-parameters for the method
- ManagementBaseObject inParams =
- repoClass.GetMethodParameters("Create");
- // Add the input parameters.
- inParams["Name"] = "Repo1";
- // Execute the method and obtain the return values.
- ManagementBaseObject outParams =
- repoClass.InvokeMethod("Create", inParams, null);
3)設置SID S-1-5-32-545 ('BUILTIN\Users') Windows用戶組對'Test' repository的讀/寫權限. FYI: AccessLevel 值在MOF有描述: "0 - 無權限, 1 - 只讀, 2 - 讀/寫".
[csharp] view plaincopyprint?
- ManagementClass userClass = newManagementClass("root\\VisualSVN","VisualSVN_WindowsAccount",null);
- ManagementClass authzClass = newManagementClass("root\\VisualSVN","VisualSVN_SecurityDescriptor",null);
- ManagementClass permClass = newManagementClass("root\\VisualSVN","VisualSVN_PermissionEntry",null);
- ManagementObject userObject = userClass.CreateInstance();
- userObject.SetPropertyValue("SID","S-1-5-32-545");
- ManagementObject permObject = permClass.CreateInstance();
- permObject.SetPropertyValue("Account", userObject);
- permObject.SetPropertyValue("AccessLevel", 2);
- ManagementObject repo = newManagementObject("VisualSVN_Repository.Name='Test'");
- ManagementBaseObject inParams =
- authzClass.GetMethodParameters("SetSecurity");
- inParams["Object"] = repo;
- inParams["Permissions"] = newobject[] { permObject };
- ManagementBaseObject outParams =
- authzClass.InvokeMethod("SetSecurity", inParams, null);
封裝後的Helper類:
[csharp] view plaincopyprint?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Management;//調用windows WMI需引入此類庫
-
- namespace Whir.Software.InteralServerManage.SVNManager
- {
- /// <summary>
- /// Visual SVN 操作輔助類:使用WMI控制VisualSVN,MOF文件存在位置:C:\Program Files\VisualSVN Server\WMI\VisualSVNServer.mof
- /// </summary>
- public class SVNHelper
- {
- #region 設置
-
- #region 設置倉庫權限
-
- public enum AccessLevel : uint
- {
- NoAccess = 0,
- ReadOnly,
- ReadWrite
- }
-
- /// <summary>
- /// 設置倉庫權限(給用戶授權)
- /// </summary>
- /// <param name="userName">用戶名</param>
- /// <param name="repository">SVN倉庫</param>
- public static bool SetRepositoryPermission(string userName, string repository)
- {
- return SetRepositoryPermission(userName, repository, 2);
- }
-
- /// <summary>
- /// 設置倉庫權限(給用戶授權)
- /// </summary>
- /// <param name="users">用戶名</param>
- /// <param name="repository">SVN倉庫</param>
- public static bool SetRepositoryPermission(List<string> users, string repository)
- {
- string userNames = "";
- foreach (string user in users)
- {
- userNames += user + ",";
- }
- return SetRepositoryPermission(userNames, repository, 2);
- }
-
- /// <summary>
- /// 設置倉庫權限(給用戶授權)
- /// </summary>
- /// <param name="userName">用戶名</param>
- /// <param name="repository">SVN倉庫</param>
- /// <param name="permission"> 權限碼:0拒絕,1只讀,2讀寫</param>
- public static bool SetRepositoryPermission(string userName, string repository, int permission)
- {
- try
- {
- string[] users = userName.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries);
- IDictionary<string, AccessLevel> permissions = GetPermissions(repository, "/");
- foreach (string s in users)
- {
- if (!permissions.ContainsKey(s))
- {
- permissions.Add(s, AccessLevel.ReadWrite);
- }
- }
- SetPermissions(repository, "/", permissions);
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
-
- /// <summary>
- /// 根據倉庫名取得倉庫實體
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- private static ManagementObject GetRepositoryObject(string name)
- {
- return new ManagementObject("root\\VisualSVN", string.Format("VisualSVN_Repository.Name='{0}'", name), null);
- }
-
- /// <summary>
- /// 讀取權限實體
- /// </summary>
- /// <param name="sid"></param>
- /// <param name="accessLevel"></param>
- /// <returns></returns>
- private static ManagementObject GetPermissionObject(string sid, AccessLevel accessLevel)
- {
- var accountClass = new ManagementClass("root\\VisualSVN",
- "VisualSVN_WindowsAccount", null);
- var entryClass = new ManagementClass("root\\VisualSVN",
- "VisualSVN_PermissionEntry", null);
- ManagementObject account = accountClass.CreateInstance();
- if (account != null) account["SID"] = sid;
- ManagementObject entry = entryClass.CreateInstance();
- if (entry != null)
- {
- entry["AccessLevel"] = accessLevel;
- entry["Account"] = account;
- return entry;
- }
- return null;
- }
-
- /// <summary>
- /// 設置倉庫權限
- /// </summary>
- /// <param name="repositoryName"></param>
- /// <param name="path"></param>
- /// <param name="permissions"></param>
- private static void SetPermissions(string repositoryName, string path,
- IEnumerable<KeyValuePair<string, AccessLevel>> permissions)
- {
- ManagementObject repository = GetRepositoryObject(repositoryName);
- ManagementBaseObject inParameters = repository.GetMethodParameters("SetSecurity");
- inParameters["Path"] = path;
- IEnumerable<ManagementObject> permissionObjects =
- permissions.Select(p => GetPermissionObject(p.Key, p.Value));
- inParameters["Permissions"] = permissionObjects.ToArray();
- repository.InvokeMethod("SetSecurity", inParameters, null);
- }
-
- /// <summary>
- /// 讀取倉庫權限
- /// </summary>
- /// <param name="repositoryName"></param>
- /// <param name="path"></param>
- /// <returns></returns>
- private static IDictionary<string, AccessLevel> GetPermissions(string repositoryName, string path)
- {
- ManagementObject repository = GetRepositoryObject(repositoryName);
- ManagementBaseObject inParameters = repository.GetMethodParameters("GetSecurity");
- inParameters["Path"] = path;
- ManagementBaseObject outParameters = repository.InvokeMethod("GetSecurity", inParameters, null);
-
- var permissions = new Dictionary<string, AccessLevel>();
-
- if (outParameters != null)
- foreach (ManagementBaseObject p in (ManagementBaseObject[]) outParameters["Permissions"])
- {
- // NOTE: This will fail if VisualSVN Server is configured to use Subversion
- // authentication. In that case you'd probably want to check if the account
- // is a VisualSVN_WindowsAccount or a VisualSVN_SubversionAccount instance
- // and tweak the property name accordingly.
- var account = (ManagementBaseObject) p["Account"];
- var sid = (string) account["SID"];
- var accessLevel = (AccessLevel) p["AccessLevel"];
-
- permissions[sid] = accessLevel;
- }
-
- return permissions;
- }
-
- #endregion
-
- #region 創建用戶組
-
- /// <summary>
- /// 創建用戶組
- /// </summary>
- public static bool CreatGroup(string groupName)
- {
- try
- {
- var svn = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);
- ManagementBaseObject @params = svn.GetMethodParameters("Create");
- @params["Name"] = groupName.Trim();
- @params["Members"] = new object[] {};
- svn.InvokeMethod("Create", @params, null);
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
-
- #endregion
-
- #region 創建用戶
-
- /// <summary>
- /// 創建用戶
- /// </summary>
- /// <param name="userName"></param>
- /// <param name="password"></param>
- /// <returns></returns>
- public static bool CreateUser(string userName, string password)
- {
- try
- {
- var svn = new ManagementClass("root\\VisualSVN", "VisualSVN_User", null);
- ManagementBaseObject @params = svn.GetMethodParameters("Create");
- @params["Name"] = userName.Trim();
- @params["Password"] = password.Trim();
- svn.InvokeMethod("Create", @params, null);
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
-
- #endregion
-
- #region 創建svn倉庫
-
- /// <summary>
- /// 創建svn倉庫
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static bool CreateRepository(string name)
- {
- try
- {
- var svn = new ManagementClass("root\\VisualSVN", "VisualSVN_Repository", null);
- ManagementBaseObject @params = svn.GetMethodParameters("Create"); //創建方法參數引用
- @params["Name"] = name.Trim(); //傳入參數
- svn.InvokeMethod("Create", @params, null); //執行
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
-
- #endregion
-
- #region 創建svn倉庫目錄
-
- /// <summary>
- /// 創建svn倉庫目錄
- /// </summary>
- /// <param name="repositories"> </param>
- /// <param name="name"></param>
- /// <returns></returns>
- public static bool CreateRepositoryFolders(string repositories, string[] name)
- {
- try
- {
- var repository = new ManagementClass("root\\VisualSVN", "VisualSVN_Repository", null);
- ManagementObject repoObject = repository.CreateInstance();
- if (repoObject != null)
- {
- repoObject.SetPropertyValue("Name", repositories);
- ManagementBaseObject inParams = repository.GetMethodParameters("CreateFolders");
- inParams["Folders"] = name;
- inParams["Message"] = "";
- repoObject.InvokeMethod("CreateFolders", inParams, null);
- }
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
-
- #endregion
-
- //public static bool SetMemberGroup(string userName, string groupName)
- //{
- // try
- // {
- // var userObj = GetGroupUsersArr(groupName);
- // foreach (ManagementBaseObject baseObject in userObj)
- // {
- // if (baseObject["Name"].ToString().ToLower() == userName)
- // {
- // return false;
- // }
- // }
- // var addUser = new ManagementClass("root\\VisualSVN", "VisualSVN_SubversionAccount", null).CreateInstance();
- // if (addUser != null)
- // {
- // addUser.SetPropertyValue("Name", userName);
- // userObj.Add(addUser);
- // }
-
- // var svnUser = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);
- // ManagementBaseObject inParams = svnUser.GetMethodParameters("SetMembers");
- // inParams["Members"] = new object[] { userObj };
- // svnUser.InvokeMethod("SetMembers", inParams, null);
- // return true;
- // }
- // catch (Exception)
- // {
- // return false;
- // }
- //}
-
- #endregion
-
- #region 讀取
-
- /// <summary>
- /// 讀取指定組裡的用戶
- /// </summary>
- /// <param name="groupName"></param>
- /// <returns></returns>
- public static List<string> GetGroupUsers(string groupName)
- {
- var listUsers = new List<string>();
- var group = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);
- ManagementObject instance = group.CreateInstance();
- if (instance != null)
- {
- instance.SetPropertyValue("Name", groupName.Trim());
- ManagementBaseObject outParams = instance.InvokeMethod("GetMembers", null, null); //通過實例來調用方法
- if (outParams != null)
- {
- var members = outParams["Members"] as ManagementBaseObject[];
- if (members != null)
- {
- foreach (ManagementBaseObject member in members)
- {
- object name = member["Name"];
- listUsers.Add(name.ToString());
- }
- }
- }
- }
- return listUsers;
- }
-
- public static List<ManagementBaseObject> GetGroupUsersArr(string groupName)
- {
- var list = new List<ManagementBaseObject>();
- var group = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);
- ManagementObject instance = group.CreateInstance();
- if (instance != null)
- {
- instance.SetPropertyValue("Name", groupName.Trim());
- ManagementBaseObject outParams = instance.InvokeMethod("GetMembers", null, null); //通過實例來調用方法
- if (outParams != null)
- {
- var members = outParams["Members"] as ManagementBaseObject[];
- if (members != null)
- {
- foreach (ManagementBaseObject member in members)
- {
- list.Add(member);
- }
- }
- }
- }
- return list;
- }
-
- #endregion
- }
- }
參考文章:
1.http://msdn.microsoft.com/en-us/library/bb404655
2.http://stackoverflow.com/questions/10017282/control-visualsvn-server-from-c-sharp
C:\
可以的
參考這個對C盤進行清理:
1.關閉系統還原:我的電腦屬性/系統還原/關閉所有磁盤上的系統還原,但是以後就不能用系統還原了!
2.關閉系統休眠:控制面板/電源/休眠/在啟動系統休眠前面的勾去掉
3.移動虛擬內存,我的電腦屬性/高級/性能/設置/高級/更改/選C盤也就是系統盤,選無分頁面,然後把虛擬內存設置到其磁盤,要剩余磁盤空間多的磁盤,比如D,E,F等盤. 設成內存的1.5~2.5倍,大小可設成一樣!
5.清理IE臨時文件夾,internet選項,刪除臨時文件和脫機文件
6.刪除系統日志和程序日志,我的電腦/控制面板/管理工具/計算機管理/事件查看器/應用程序,鼠標右鍵/清除所事件,在依次清除系統日志
7.清理系統緩存:2000系統是:C:\WINNT\system32\dllcache下的所有文件
XP系統是:C:\windows\system32\dllcache下的所有文件 清理系統緩存(打開我的電腦/工具/文件和文件夾選項/隱藏受保護的系統文件的勾去掉在把顯示全部文件勾上)。也可以直接運行sfc.exe /purgecache命令自動刪除。
8.清空回收站
9.刪除c:\windows\SoftwareDistribution\Download下的文件(系統更新時下載的文件如你裝好了更新也就沒有用了)
10.刪除c:\windows\RegisteredPackages下所有目錄
11.刪除C:\WINDOWS\Downloaded Program Files下所有的文件
12.我的電腦 文件夾選項 查看 隱藏已知受系統保護的文件勾去掉,顯示所有文件勾上確定。
13.刪除c:\windows\所有帶$8882305$的文件(系統更新後的備份文件)
zhidao.baidu.com/question/11035955.html
zhidao.baidu.com/question/12223613.html
zhidao.baidu.com/question/14874715.html
......余下全文>>
C:\
可以的
參考這個對C盤進行清理:
1.關閉系統還原:我的電腦屬性/系統還原/關閉所有磁盤上的系統還原,但是以後就不能用系統還原了!
2.關閉系統休眠:控制面板/電源/休眠/在啟動系統休眠前面的勾去掉
3.移動虛擬內存,我的電腦屬性/高級/性能/設置/高級/更改/選C盤也就是系統盤,選無分頁面,然後把虛擬內存設置到其磁盤,要剩余磁盤空間多的磁盤,比如D,E,F等盤. 設成內存的1.5~2.5倍,大小可設成一樣!
5.清理IE臨時文件夾,internet選項,刪除臨時文件和脫機文件
6.刪除系統日志和程序日志,我的電腦/控制面板/管理工具/計算機管理/事件查看器/應用程序,鼠標右鍵/清除所事件,在依次清除系統日志
7.清理系統緩存:2000系統是:C:\WINNT\system32\dllcache下的所有文件
XP系統是:C:\windows\system32\dllcache下的所有文件 清理系統緩存(打開我的電腦/工具/文件和文件夾選項/隱藏受保護的系統文件的勾去掉在把顯示全部文件勾上)。也可以直接運行sfc.exe /purgecache命令自動刪除。
8.清空回收站
9.刪除c:\windows\SoftwareDistribution\Download下的文件(系統更新時下載的文件如你裝好了更新也就沒有用了)
10.刪除c:\windows\RegisteredPackages下所有目錄
11.刪除C:\WINDOWS\Downloaded Program Files下所有的文件
12.我的電腦 文件夾選項 查看 隱藏已知受系統保護的文件勾去掉,顯示所有文件勾上確定。
13.刪除c:\windows\所有帶$8882305$的文件(系統更新後的備份文件)
zhidao.baidu.com/question/11035955.html
zhidao.baidu.com/question/12223613.html
zhidao.baidu.com/question/14874715.html
......余下全文>>