improve changelog
[tfs.git] / sample.cs
blob38621fdb73a0121bee58dc86e8a937591d11106b
1 using System;
2 using System.Globalization;
3 using System.IO;
4 using System.Net;
5 using Microsoft.TeamFoundation;
6 using Microsoft.TeamFoundation.Client;
7 using Microsoft.TeamFoundation.VersionControl.Client;
8 using Microsoft.TeamFoundation.VersionControl.Common;
10 namespace DirSize
12 class Program
14 static void Main(string[] args)
16 // Check and get the arguments.
17 String path;
18 RecursionType recursion;
19 VersionControlServer sourceControl;
20 GetPathAndRecursion(args, out path, out recursion, out sourceControl);
22 Item[] items = null;
23 try
25 // Get the latest version of the information for the items.
26 Console.WriteLine("path is {0}", path);
27 ItemSet itemSet = sourceControl.GetItems(path, recursion);
28 items = itemSet.Items;
30 catch (TeamFoundationServerException e)
32 // We couldn't contact the server, the item wasn't found,
33 // or there was some other problem reported by the server,
34 // so we stop here.
35 Console.Error.WriteLine("Exception: {0}", e.Message);
36 Environment.Exit(1);
39 if (items.Length == 0)
41 Console.WriteLine("There are no items for " + path);
43 else
45 Array.Sort(items, Item.Comparer);
47 long totalBytes = 0;
48 int numFiles = 0, numFolders = 0;
49 String format = "{0,-25} {1,-8} {2, 8} {3}";
50 foreach (Item item in items)
52 if (item.ItemType == ItemType.File)
54 // In addition to the information printed, the Item object has
55 // the changeset version and MD5 hash of the file content as
56 // properties on item.
57 numFiles++;
58 totalBytes += item.ContentLength;
59 Console.WriteLine(format,
60 item.CheckinDate.ToLocalTime().ToString("G"),
61 String.Empty,
62 item.ContentLength,
63 item.ServerItem);
65 else
67 numFolders++;
68 Console.WriteLine(format,
69 item.CheckinDate.ToLocalTime().ToString("G"),
70 "<DIR>",
71 String.Empty,
72 item.ServerItem);
76 Console.WriteLine();
77 Console.WriteLine("{0} files, {1} folders, {2} bytes",
78 numFiles, numFolders, totalBytes);
82 private static void GetPathAndRecursion(String[] args,
83 out String path, out RecursionType recursion,
84 out VersionControlServer sourceControl)
86 if (args.Length < 3)
88 Console.WriteLine("Usage: dirsize <domain username password>");
89 Environment.Exit(1);
92 // Figure out the server based on either the argument or the
93 // current directory.
94 WorkspaceInfo wsInfo = null;
95 path = Environment.CurrentDirectory;
97 if (wsInfo == null)
99 wsInfo = Workstation.Current.GetLocalWorkspaceInfo(Environment.CurrentDirectory);
102 // Stop if we couldn't figure out the server.
103 if (wsInfo == null)
105 Console.Error.WriteLine("Unable to determine the server.");
106 Environment.Exit(1);
109 Console.WriteLine("Server: {0}", wsInfo.ServerUri.AbsoluteUri);
111 TeamFoundationServer tfs = new TeamFoundationServer(wsInfo.ServerUri.AbsoluteUri,
112 new NetworkCredential(args[1], args[2], args[0]));
114 sourceControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
116 // Pick up the recursion, if supplied. By default,
117 // we want item and its immediate children, if it is a folder.
118 // If the item is a file, then only the file will be returned.
119 recursion = RecursionType.OneLevel;
120 if (args.Length == 1 && args[0] == "/r" ||
121 args.Length == 2 && args[1] == "/r")
123 recursion = RecursionType.Full;