0

I've built a version info command into my c# dll using the very helpful info here: Displaying the build date

I actually output a range into excel, which displays some text, the assembly info and build date/time etc. Ideally I'd it say something like "Built on 23/08/2013 at 10:24:30 by username". How can I get that username (login id is fine) ?

Thanks Craig

--- In response to comments please see further info below - the above question still stands

Hi If it helps to make things clearer for people who may be interested in this thread I will describe the situation at my company: There will probably be at most 3 people developing the code, all compiled with VS and we work exclusively on a windows system (8). The user base is at most 20 people. The codes are version controlled via svn and things are at a prototype stage so we haven't got into an official release cycle. However during the day the 3 of us will most likely make a number of changes and release to selected users, hence at some point the versions between the users could get out of sync. I want to know if a user comes complaining to me that something doesn't work who gave them that version. If it doesn't work then its likely that the person who changed & compiled the code did some changes on their own stuff and didn't run the regression tests and released the code to some users. I'd like to know very quickly who did the coding & release/compilation via the method I am asking the question for here so I can go and find them and give them a slap, without having to dig through the Svn change logs (which may not even have changed because the changes are not checked in). So hopefully that is now totally clear - a rather informal arrangement I agree, but it works in the environment we work in. No lectures on code release cycles etc. please, I'm sure the solution is simple enough and I'm not sure how a change of thread title would help - it is exactly what I want to do. Thanks Craig

Community
  • 1
  • 1
  • 1
    Compiler username? You mean a windows username? – nan Aug 23 '12 at 09:29
  • Nope I mean the user who compiled the code - not the current user of the system. – Craig Robinson Aug 23 '12 at 09:52
  • From your comment below surely it would be more useful to know who **wrote** the code instead of who compiled it? unless only one person works on each project you won't know who actually caused the error, if only one person does work on each project then you already know who wrote it? – James Aug 23 '12 at 10:19
  • In that case this entirely depends on the build system, VCS etc it's not something Windows can supply (or cares about). I think you need to edit the title of this post and the description somewhat. – Lloyd Aug 23 '12 at 10:54

2 Answers2

1

Though you have already implemented version info by reading PE header, that was not strictly required.

To solve your problem without reading PE header, you can:

  1. Define a custom attribute

    [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
    internal sealed class BuildInfoAttribute : Attribute 
    {
        public string UserName
        {
            get; private set;
        }
    
        public string BuildDate
        {
            get; private set;
        }
    
        public BuildInfoAttribute(string username, string buildDate)
        {
            UserName = username;
            BuildDate = buildDate;
        }
    }
    
  2. Include the attribute in AssemblyInfo.cs

    [assembly: BuildInfo("AmitMittal", "08/23/2012 17:40:16")]
    
  3. Download and install MSBuild Community Tasks. Remember to chose option to integrate with VS

  4. Open .csproj file in a text editor and include the following lines in it before the </Project> tag

    <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
    <PropertyGroup>
      <UseHostCompilerIfAvailable>FALSE</UseHostCompilerIfAvailable>
    </PropertyGroup>
    <Target Name="BeforeBuild">
    <Time>
      <Output PropertyName="BuildDateTime" TaskParameter="FormattedTime" />
    </Time>
    <FileUpdate Files="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs" Regex="BuildInfo\(.*" ReplacementText="BuildInfo(&quot;$(USERNAME)&quot;,&quot;$(BuildDateTime)&quot;)]" Condition="'$(Configuration)' == 'Release'" />
    </Target>
    
  5. Build in Release mode to time-stamp your project

  6. Get BuildInfo as following:

    var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof (BuildInfoAttribute), false);
    BuildInfoAttribute attribute = (BuildInfoAttribute) attributes[0];
    Console.WriteLine("Built by " + attribute.UserName + " on " + attribute.BuildDate);
    
Amit Mittal
  • 2,646
  • 16
  • 24
  • @user1619270 You are welcome :). Since you seem to be a new SO user, would just like to mention here that if this answer solves your problem, do mark it as accepted answer. – Amit Mittal Aug 23 '12 at 12:33
  • Amit - I'm afraid I got stuck at the point #3. I downloaded the zip file, and extracted but then it wasn't clear to me what I'm supposed to do to install ? There's no msi file in my extraction ? – Craig Robinson Aug 23 '12 at 17:28
  • @user1619270 you can simply download the msi version and install it. It is available on the same page. – Amit Mittal Aug 24 '12 at 06:20
  • Hi - you're absolutely right, apologies I just grabbed the first thing I saw, was half asleep yesterday. It now works a treat. I'm not usually a fan of using 3rd party projects, I'd prefer something self contained - what exactly is the provenance of the MSBuild project ? – Craig Robinson Aug 24 '12 at 08:16
  • If you want your solution to be free of 3rd party dlls, you can very easily create your own specialized MSBuild task (http://msdn.microsoft.com/en-us/library/microsoft.build.utilities.task(v=vs.100).aspx) and then use that task instead of relying upon MSBuild Community Tasks. – Amit Mittal Aug 24 '12 at 09:21
  • Time and FileUpdate are just classes that extends the above mentioned abstract class 'Task'. Time class exposes a property 'FormattedTime' that returns current time formatted using InvariantCulture. FileUpdate class, as you may guess from the name, updates a file by searching for text matching the specified regex and replacing it with specified replacement text. USERNAME is a predefined variable while we are defining BuildDateTime variable ourself. – Amit Mittal Aug 24 '12 at 09:34
0

You could use Environment.UserName:

var username = Environment.Username;

See here for documentation - http://msdn.microsoft.com/en-us/library/system.environment.username.aspx

You can also use WindowsIdentity. In this instance if I recall it also includes the domain name (where applicable):

var username = WindowsIdentity.GetCurrent().Name;
Lloyd
  • 29,197
  • 4
  • 84
  • 98
  • As I understand it this will report back the current user of the "system". I want to report back the user who actually compiled the code, so someone can go and beat them if something goes wrong. I guess there could be a way that you could fudge it using some post build event into a static variable or some such, but the method outlined in the link for the build time date of the linker enables no such fudge. I guess one thing to ask is "is the compiler user stored in the linker file" ? – Craig Robinson Aug 23 '12 at 10:07