Click here to Skip to main content
15,885,366 members
Articles / Versioning
Tip/Trick

Automatic Versioning - How I Do It

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
3 Jun 2022CPOL2 min read 9.3K   3   4
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)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
SuggestionUse AssemblyVersion("1.0.*") Pin
KevinAG6-Jun-22 13:45
KevinAG6-Jun-22 13:45 
GeneralRe: Use AssemblyVersion("1.0.*") Pin
#realJSOP6-Jun-22 22:58
mve#realJSOP6-Jun-22 22:58 
GeneralRe: Use AssemblyVersion("1.0.*") Pin
KevinAG7-Jun-22 7:47
KevinAG7-Jun-22 7:47 
Suggestion.NET Core versioning Pin
RickZeeland4-Jun-22 22:22
mveRickZeeland4-Jun-22 22:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.