private void Checkout(IVSSItem checkoutItem, FileInfo localPath, int flags, string pattern) { //TODO: timestamp stuff switch (checkoutItem.Type) { case (int)VSSItemType.VSSITEM_PROJECT: // Item is a project. if (string.IsNullOrEmpty(pattern)) { // In the absence of a pattern, we'll checkout the entire project. checkoutItem.Checkout("", localPath.FullName, flags); } else { // If a pattern is provided, we process all subitems in the project. foreach (IVSSItem subItem in checkoutItem.get_Items(false)) { switch (subItem.Type) { case (int)VSSItemType.VSSITEM_PROJECT: // Subitem is a project. if (Recursive) { // We'll recursively checkout matching files in this project. Checkout(subItem, new FileInfo(System.IO.Path.Combine(localPath.FullName, subItem.Name)), flags, pattern); } break; case (int)VSSItemType.VSSITEM_FILE: // Subitem is a file. if (Regex.IsMatch(subItem.Name, pattern)) { // We'll checkout this file since it matches the search pattern. Checkout(subItem, localPath, flags, ""); } break; } } } break; case (int)VSSItemType.VSSITEM_FILE: // Item is a file. string filePath = System.IO.Path.Combine(localPath.FullName, checkoutItem.Name); checkoutItem.Checkout("", filePath, flags); break; } }
/// <summary> /// vss check out命令 /// </summary> /// <param name="vssFilePath">vss上的文件路径</param> /// <param name="localPath">本地文件路径</param> /// <param name="comment">checkout注释</param> public void CheckOut(string vssFilePath, string localPath, string comment) { vssFilePath = this.FormatToVssDir(vssFilePath); localPath = this.FormatToLocalDir(localPath); //string testFile = "$/TestFolder/test.txt"; // Create a VSSDatabase object. IVSSDatabase vssDatabase = new VSSDatabase(); // Open a VSS database using network name // for automatic user login. vssDatabase.Open(this.srcSafeIni, this.username, this.password); IVSSItem vssFile = vssDatabase.get_VSSItem(vssFilePath, false); vssFile.Checkout(comment, localPath, 0); if ((VSSFileStatus)vssFile.IsCheckedOut == VSSFileStatus.VSSFILE_NOTCHECKEDOUT) { Console.WriteLine(vssFile.Spec + " is checked in."); } else { Console.WriteLine(vssFile.Spec + " is checked out."); } Console.WriteLine("Now alter the file and hit any key."); Console.ReadLine(); }
//Unpin + Checkout + Exception handling private bool PrepareToPushFile(string vssDir, string localDir, string localFileName) { string fileName = $"{vssDir}/{localFileName}"; try { IVSSItem item = VSSDB.get_VSSItem(fileName, false); if (item.IsPinned) { Unpin((VSSItem)item); sender($"{item.Spec} unpinned"); } //file is not checked out if (item.IsCheckedOut == 0) { item.Checkout("", Path.Combine(localDir, localFileName), (int)VSSFlags.VSSFLAG_GETNO); sender($"{item.Spec} checked out"); } //file is checked out to another user else if (item.IsCheckedOut == 1) { sender($"{item.Spec} checked out by another user!"); return(false); } } catch (System.Runtime.InteropServices.COMException exc) { //file not found if (!IsFileNotFoundError(exc)) { sender($"{exc.Message}"); throw exc; } else { sender($"{fileName} не найден"); } } return(true); }