You can embed a version.txt file into the executable and then read the version.txt out of the executable. To create the version.txt file, use git describe --long
Here are the steps:
Use a Build Event to call git
-
Right-click on the project and select Properties
-
In Build Events, add Pre-Build event containing (notice the quotes):
“C:\Program Files\Git\bin\git.exe” describe –long > “$(ProjectDir)\version.txt”
That will create a version.txt file in your project directory.
Embed the version.txt in the executable
- Right click on the project and select Add Existing Item
- Add the version.txt file (change the file chooser filter to let you see All Files)
- After version.txt is added, right-click on it in the Solution Explorer and select Properties
- Change the Build Action to Embedded Resource
- Change Copy to Output Directory to Copy Always
- Add version.txt to your .gitignore file
Read the embedded text file version string
Here’s some sample code to read the embedded text file version string:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
namespace TryGitDescribe
{
class Program
{
static void Main(string[] args)
{
string gitVersion= String.Empty;
using (Stream stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("TryGitDescribe." + "version.txt"))
using (StreamReader reader = new StreamReader(stream))
{
gitVersion= reader.ReadToEnd();
}
Console.WriteLine("Version: {0}", gitVersion);
Console.WriteLine("Hit any key to continue");
Console.ReadKey();
}
}
}