Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / versioning

Automatic Versioning - How I Do It

4.33/5 (3 votes)
3 Jun 2022CPOL2 min read 11.5K  
A half-hearted approach to automatically versioning your projects
This may not be an all-encompassing way to increment your project version, but it is certainly the lowest impact and automatic way.

Introduction

I'm working on an application, and my mind drifted toward the topic of versioning. I sat their stewing over Microsoft's lack of desire (or maybe more likely, their inability) to address a common developer concern - automatically incrementing version numbers when the project is build. After a while, I came up with a way that appears to work exactly the way I want it to.

The project is a MVC 5 (.Net Framework) web application, and when I do a build, the binaries are put into the bin folder for the solution. This folder is also deployed with my application, and it is this realization that triggered the following idea.

Simply put, I *ignore* the version component defined in the assembly.cs (this is, afterall, the root of our inability to automatically increment version numbers), and instead, rely on the last-write datetime of the the binaries in the bin folder. It's really just that simple.

This works because a given assembly's file date doesn't change unless the assembly was re-compiled. So, the act of using F5 to run the app under the debugger (without changing anything) will not cause the date to change, and even more importantly, just running the app won't cause a file date change because the compiler didn't generate new assemblies.

I know, this is pretty half-assed because ther's all kinds of reasons this approach won't work (when you are new and get the latest code from source control and do a build, the version will be different from what's in production), but I never said this was the best way to do this. It's just a way to do this.

Using the code

The code is really quite simple, and I just put the method into a static class that I call Global...

C#
public static string AppVersion { get; set; }

// Using a file mask allows you to specify more than one 
// file/extension as well as utilize wildcards, which 
// makes the method usable in desktop apps as well.
public static void FindNewestBinary(string path, string fileMask)
{
    DateTime date =  new DateTime(2022, 01, 01);
    try
    {
        DirectoryInfo dir = new DirectoryInfo(path);
        FileInfo file = dir.GetFiles(fileMask)
                        .OrderByDescending(x=>x.LastWriteTime)
                        .FirstOrDefault();
        if (file != null)
        {
            date =  file.LastWriteTime;
        }
    }
    catch (Exception)
    {
    }
    int seconds = date.Hour * 60 +
                    date.Minute * 60 + 
                    date.Second;
    string fileDate = date.ToString("yyyy.MM.dd.");
    Global.AppVersion = string.Format("{0}{1:00000}", fileDate, seconds); 
}
}

...and call it from the Application_Start() method in global.asax.cs file.

C#
Global.FindNewestBinary(Server.MapPath(@"bin"), "*.dll");

Then whenever I need the version, I simply refer to the Global.AppVersion property, like so:

HTML
<span>@Global.AppVersion</span>

History

  • 2022.06.03 09:00 - Inital post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)