Archive

Archive for the ‘C#’ Category

VisualStudio TechnologyExplorer : Generate a graph of your solutions(.sln) & projects (.csproj)

 
Download: You can download the TechnologyExplorer here.
 
TechnologyExplorer Example
 
I’m developing with .NET since version 1.1 which was Visual Studio 2003 released in 2003. Since, 5 .NET framework have been released (2, 3, 3.5, 4, 4.5) with 4 VisualStudio (2005, 2008, 2010, 2012).

I have a lot of applications (web, console, windows service and WindowsForm) that used different libraries. I wanted to know the .NET version of every library/application, the VisualStudio version of every applcation and to see the dependencies between the libraries and the applications.

To see the dependencies, you can use the ArchitectureExplorer available in VisualStudio Ultimate. But you can’t extract the .NET version and the VisualStudio version with this tool.
I wanted to use the Graph layout capabilities of VisualStudio so I don’t have to program a algo to place the nodes and reduce the overlap between the links.
 

DGML

To display graphs, VisualStudio use DGML that is a xml file. So you can create manually or by a program a dgml that you can open in VisualStudio.
The specs are available at http://msdn.microsoft.com/en-us/library/ee842619.aspx.
 

Determine the VisualStudio version

The VisualStudio version is available in the sln file:

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012

 

Determine the .NET Framework version

The .net framework version, is available in the csproj under the node TargetFrameworkVersion.
 

TechnologyExplorer

I wrote a application, the TechnologyExplorer, given a base folder, navigate in thew folder and it’s subfolders to extract the .NET version and the VisualStudio version of the applications and libraries.
It generate a dependency graph that shown the applications and the libraries and their dependencies.

  • The background color indicate the type: web application, application or library
  • The outline indicate the .NET framework version
  • For applications, the text color indicate the VisualStudio version

 
The output is a DGML file that you can open in VisualStudio to see the graph.

TechnologyExplorer Example

The TechnologyExplorer also generate a Legend file also in a DGML format:
TechnologyExplorer Legend

Configuration

The TechnologyExplorer.xml contains a configuration where you indicate the output folder.

  • The outputPath attribute specify the folder where the dgml files will be generated.
  • Each Include node specify a folder to traverse.
  • Each Exclude node specify a folder, and sub-folders, to not traverse.

TechnologyExplorer - Microsoft Visual Studio

 

Generation

One the xml file configured, just launch the TechnologyExplorer.exe!

TFS SDK: List the states transitions available for a WorkItemType

Go to the TFS Content Page


When a WorkItem is in a specific state, only states that have transition from the current state are availalbe.

The following code access the WoirkItemType XML definition and extract all transitions defined. The list returned contains the from and to state transition available.

private static IList<Transition> LoadTransitions(WorkItemType workItemType)
{
   XmlDocument workItemTypeXml = workItemType.Export(false);
            
   XmlNodeList transitionsList = workItemTypeXml.GetElementsByTagName("TRANSITIONS");
   XmlNode transitionsNode = transitionsList[0];

   var transistions = new List<Transition>();
   foreach (XmlNode transition in transitionsNode)
      transistions.Add(new Transition(transition.Attributes["from"].Value, transition.Attributes["to"].Value));
   return transistions;
}
Categories: C#, TFS

TFS SDK: List the iterations

Go to the TFS Content Page


You access the iterations via the Project’s IterationRootNodes properties.
The iterations are hierachical, you have a collection of nodes that have children.

A example that return a flat list of all iterations hierarchy:

public IList<string> GetIterationPaths(Project project)
{
   List<string> iterations = new List<string>();
   foreach (Node node in project.IterationRootNodes)
      AddChildren(string.Empty, node, iterations);
   return iterations;
}

private void AddChildren(string prefix, Node node, List<string> items)
{
   items.Add(node.Path);
   foreach (Node item in node.ChildNodes)
      AddChildren(prefix + node.Name + "/", item, items);
}
Categories: C#, TFS

TFS SDK: List the areas

Go to the TFS Content Page


You access the areas via the Project’s AreaRootNodes properties.
The areas are hierachical, you have a collection of nodes that have children.

A example that return a flat list of all areas hierarchy:

public IList<string> GetAreaPaths(Project project)
{
   List<string> areas = new List<string>();
   foreach (Node node in project.AreaRootNodes)
      AddChildren(string.Empty, node, this.areas);
   return areas;
}

private void AddChildren(string prefix, Node node, List<string> items)
{
   items.Add(node.Path);
   foreach (Node item in node.ChildNodes)
      AddChildren(prefix + node.Name + "/", item, items);
}
Categories: C#, TFS

TFS SDK: List the states of a WorkItemType

Go to the TFS Content Page


The state of a WorkItemType are stored in the WorkItemType xml definition. You need to export it using the Export method as follow:

public static IList<string> GetStates(WorkItemType wit)
{
   XmlDocument doc = wit.Export(false);

   XmlNodeList nodes = doc.DocumentElement.SelectNodes("WORKITEMTYPE/WORKFLOW/STATES/STATE");
   List<string> list = new List<string>();
   foreach (XmlNode node in nodes)
   {
      string name = node.Attributes["value"].Value;
      if (!string.IsNullOrEmpty(name) && !list.Contains(name))
         list.Add(name);
   }
   return list;
}
Categories: C#, TFS

TFS SDK: WorkItemFormControl, a TFS control to view and edit a WorkItem

Go to the TFS Content Page


The TFS SDK come with a WinForm control named WorkItemFormControl that display the informations related to a WorkItem. The control allow edition.

The WorkItemFormControl is in the library Microsoft.TeamFoundation.WorkItemTracking.Controls.dll that is located under C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies.

You will need to add a reference to this library and a using on the namespace Microsoft.TeamFoundation.WorkItemTracking.Controls.

Note: You need to target the framework .NET Framework 4 not the .NET Framework 4 Client Profile. (Project properties, tab application)

Let’s create a Form named FormWorkItem:

using Microsoft.TeamFoundation.WorkItemTracking.Client;

public partial class FormWorkItem : Form
{
 public FormWorkItem(WorkItem workItem)
 {
 InitializeComponent();
 }
}

Add the namespace for the control:

using Microsoft.TeamFoundation.WorkItemTracking.Controls;

Create and bind the WorkItemFormControl:

using Microsoft.TeamFoundation.WorkItemTracking.Controls;

public partial class FormWorkItem : Form
{
 private WorkItem workItem;

 public FormWorkItem(WorkItem workItem)
 {
 this.workItem = workItem;

 InitializeComponent();

 WorkItemFormControl control = new WorkItemFormControl();
 control.Item = workItem;
 control.ReadOnly = false;
 control.Dock = DockStyle.Fill;
 this.Controls.Add(control);

 this.Text = workItem.Type.Name + ": " + workItem.Id + " - " + workItem.Title;
 }
}

Let’s add a FormClosing event to save the WorkItem:

if (this.workItem.IsDirty)
{
 DialogResult result = MessageBox.Show("Do you want to save changes?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
 if (result == System.Windows.Forms.DialogResult.Yes)
 {
 try
 {
 this.workItem.Save();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 }
 }
 else if (result == System.Windows.Forms.DialogResult.No)
 this.workItem.Reset();
 else
 e.Cancel = true;
}
Categories: C#, TFS

TFS SDK: Field list

Go to the TFS Content Page


You can access the list of fields from the WorkItemType using the FieldDefinitions properties, which is a collection of FieldDefinition.
The FieldDefinition define some properties like the Name, FieldType and other properties that define the behaviour of the field (CanSortBy, IsComputed, IsEditable, Isqueryable, etc).

WorkItemType wit = ...;
foreach (FieldDefinition fd in wit.FieldDefinitions)
   Debug.WriteLine(fd.Name + "\t" + fd.FieldType);
Categories: C#