Retrieving an Assembly’s Build Path

If you work in a team of several developers, and often come across the question, “Who built this latest version and when?”, here’s how you find out.

If an assembly if compiled with debug information, the StackFrame class contains information about its absolute source file names.

  public static class BuildInfo
  {
    private static string sBuildPath;

    public static string BuildPath
    {
      get
      {
        if (!string.IsNullOrEmpty(sBuildPath))
          return sBuildPath;

        StackFrame sf = new StackFrame(true);
        string s = sf.GetFileName();
        if (!string.IsNullOrEmpty(s))
        {
          int i = s.LastIndexOf("\\");
          if (i == -1)
            sBuildPath = s;
          else
            sBuildPath = s.Substring(0, i);
        }
        else
          sBuildPath = ".";

        return sBuildPath;
      }
    }
  }

If the source files are kept in directories named after the developer (e.g. C:\Team\DeveloperName\Project), it is easy to find out who last released a test version of an application.

To find out the build timestamp of an assembly, use these few lines:

  public static class BuildInfo
  {
    public static DateTime BuildDate
    {
      get
      {
        System.Reflection.Assembly assembly =
          System.Reflection.Assembly.GetExecutingAssembly();
        System.IO.FileInfo fileInfo = 
          new System.IO.FileInfo(assembly.Location);
        return fileInfo.LastWriteTime;
      }
    }
  }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: