views:

386

answers:

7

I have a C# WinForms app with an About box. I am putting the version number in the about box using:

FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location)
    .FileVersion

This ends up giving me the Subversion revision number from which the executable was built.

I would also like to get the date of the build into the About box. I tried:

File.GetLastWriteTime(Assembly.GetEntryAssembly().Location)

But that gives me the write date of the executable, which only corresponds to the date when the app was installed (we are using ClickOnce) not built.

How can I get the build date?

+3  A: 

If you use automatic versioning, you can convert the last two bits of the version number into a build date: MSDN

Michael Donohue
make your version number a date based system, set by your continuous integration, and parse it back for the date.
DevelopingChris
msbuild/visual studio will do this for you, read the first bit of the linked article.
Michael Donohue
A: 

The only way i was able to do it in C/C++ was to actually have a post-build process do a search and replace of a special string allocated as a "static const" in the binary.

There might be an easier way in c# though.

cyberconte
A: 

You could change your assembly versioning to encode the date, but that would probably mean losing your subversion revision information which is arguably more useful.

This should work: write the current date/time into a .cs file as a pre-build task like so:

[assembly: AssemblyCreated(CreatedDate = new DateTime(...))]

You could use a batch file, PowerShell script or executable for that.

Include the file in your project (build action: compile) and include the custom attribute:

[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
public sealed class AssemblyCreatedAttribute : Attribute
{
    public DateTime CreatedDate { get; set; }
}

On application start you can use reflection to get the custom attribute from the assembly for display in the about page.

Matt Howells
A: 

I don't think a standard way of doing it exists, but you can roll something up yourself. Create a custom assembly-level attribute, give it a cool name like 'AssemblyDateAttribute'. Let it take a string that you can parse into a DateTime in the constructor that is accessible via a property.

As part of a build process, create a new file with only the attribute being applied to the assembly. (Make it look like AssemblyInfo.cs) and then include that in your build input.

Then in your about box, search your assembly for instances of this attribute and display the date value in your box.

Pieter Breed
+1  A: 

We're using this very similiar piece of code:

DateTime buildDate = new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime;

and I'm pretty sure it doesn't change when installing from ClickOnce.. If I'm wrong please correct me!

Oskar
Is there a setting in ClickOnce to make sure it doesn't update the file dates on each install?
JoelFan
Actually, in my case ClickOnce is only updating the file dates for my project's .exe and .dll files ... the 3rd party dll's have older dates
JoelFan
I just remembered, this is where I got the code: http://stackoverflow.com/questions/613700/how-to-put-the-build-date-of-application-somewhere-in-the-application
Oskar
A: 
Assembly.GetExecutingAssembly().GetName().Version.ToString();

From my own C# code.

mcandre
where is the date?
JoelFan
A: 

In Visual Studio projects there is a file AssemblyInfo.cs, but you can use any other .cs file. Look at the attribute AssemblyVersion:

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number 
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.*")]

Now you can calculate the build date by using the Version.Build property. The value that Version.Build returns is the number of days since: 2000/1/1

GvS