If you ever want to pull a version number out of a file’s name (like a NuGet package, for example), it is pretty easy with regex. Of course, I suck at regex. I always need to look up the syntax and even then I probably won’t get it right. So, anyway, here is the code for looking for a nupkg file and returning its version number. It also works with pre-release packages (ones with a suffix like “-alpha”).
[code language=”csharp”]
string versionNumber = "1.0.0"; // Default version number in case the following fails
var nupkgFile = Directory.GetFiles(".", "*.nupkg", SearchOption.TopDirectoryOnly).FirstOrDefault();
if (!String.IsNullOrWhiteSpace(nupkgFile))
{
var match = Regex.Match(nupkgFile, @"(*|d+(.d+)*(.*)?)(-[A-Z,a-z,0-9]*)?");
if (match.Success)
versionNumber = match.Value;
}
[/code]