0

I am working on a winforms application and we deploy the dlls on a DEV server many times a day. We want to be able to find out who built the dll.

I have tried adding System.Security.Principal.WindowsIdentity.GetCurrent().Name to assembly info but it takes only const.

Is there some way to embed username into the dll during build ?

Dotnay Tupperson
  • 172
  • 1
  • 10

1 Answers1

1

StackOverflow and Coding Horror have examples of creating custom assembly attributes. Based on those examples, you could create something like:

[AttributeUsage(AttributeTargets.Assembly)]
public class AssemblyBuildSystem : System.Attribute
{
    private string _strBuildSystemName;

    public AssemblyBuildSystem(string buildSystemName)
    {
        _strBuildSystemName = buildSystemName;
    }

    public BuildSystemName { get { return _strBuildSystemName; } }
}

That will give you a custom "AssemblyBuildSystemName" attribute that you can examine via reflection. The problem will be making sure that it's correct at each build, since an attribute can only take constant parameters.

You can add the attribute to the assembly as normal:

[Assembly: AssemblyBuildSystemName("Bob's Development Machine")]

The downside is that you don't want this to be source-controlled, so it probably should reside in a non-source-controlled .cs file specific to each developer's machine. You'll have to rely on each developer to create the file, make sure it's not source-controlled, and make sure that the content is accurate.

You might be able to modify the project target to pass the hostname in as a conditional compilation constant, or to create and add that file as a pre-build step, but at some point it will become easier to go with a build server or modify your deployment process.

Community
  • 1
  • 1
pmcoltrane
  • 3,052
  • 1
  • 24
  • 30
  • Our team is quite big and we cant rely on developers to add their name into a file. We want something that will read the user id at build time and embed it into the dll. Is there something we can do with the .resx file ? – Dotnay Tupperson Jun 17 '14 at 17:26
  • Not that I can think of offhand. If you want it in AssemblyInfo, you need to define an attribute, and attribute parameters must be constant. The best I think you could do is to create a script that reads the current user name and updates the .cs file, then run it as a pre-build script. – pmcoltrane Jun 17 '14 at 17:45
  • I created a new console application that reads the assemblyinfo.cs and modifies an attribues (adds the persons usersname who compiled the dll) and added the exe of this new application to the prebuild event of my windows application. So now when someone compiles the dll their assemblyinfo.cs file is updated with their login name and then the dll is built and on runtime we can read the attribute and get the name of person who built the dll. – Dotnay Tupperson Jun 19 '14 at 20:24