Using Static Methods from the .NET Framework in MSBuild – a List of All Property Functions

by Oliver 9. June 2016 20:20

In the MSBuild deployment script for our discoverize portals we use a number of useful functions from the .NET framework, e.g.:

  • $([System.IO.File]::Exists($file))
  • $([System.IO.Path]::GetFileName($(Destination)))
  • $([System.IO.Directory]::GetDirectories("$(Folder)"))
  • $([System.DateTime]::Now.ToString($(TimestampFormat)))

These methods are called Property Functions and have been made available for use in MSBuild scripts since version 4.

Here's the full list of .NET framework types whose static methods or properties you can use almost anywhere in you MSBuild scripts:

image

  • System.Byte
  • System.Char
  • System.Convert
  • System.DateTime
  • System.Decimal
  • System.Double
  • System.Enum
  • System.Guid
  • System.Int16
  • System.Int32
  • System.Int64
  • System.IO.Path
  • System.Math
  • System.UInt16
  • System.UInt32
  • System.UInt64
  • System.SByte
  • System.Single
  • System.String
  • System.StringComparer
  • System.TimeSpan
  • System.Text.RegularExpressions.Regex
  • Microsoft.Build.Utilities.ToolLocationHelper

There are a few rather useful methods from some more types that you can also use:

  • System.Environment::CommandLine
  • System.Environment::ExpandEnvironmentVariables
  • System.Environment::GetEnvironmentVariable
  • System.Environment::GetEnvironmentVariables
  • System.Environment::GetFolderPath
  • System.Environment::GetLogicalDrives
  • System.IO.Directory::GetDirectories
  • System.IO.Directory::GetFiles
  • System.IO.Directory::GetLastAccessTime
  • System.IO.Directory::GetLastWriteTime
  • System.IO.Directory::GetParent
  • System.IO.File::Exists
  • System.IO.File::GetCreationTime
  • System.IO.File::GetAttributes
  • System.IO.File::GetLastAccessTime
  • System.IO.File::GetLastWriteTime
  • System.IO.File::ReadAllText

The general pattern to call a property method is:

$([Class]::Method(Parameters)).

Beyond the above mentioned methods, MSBuild offers some more helpful ones that are invoked on the MSBuild pseudo class:

  • [MSBuild]::DoesTaskHostExist(string runtime, string arch)
  • [MSBuild]::GetDirectoryNameOfFileAbove(string p, string f)
  • [MSBuild]::GetRegistryValue(...)
  • [MSBuild]::GetRegistryValueFromView(...)
  • [MSBuild]::MakeRelative(string path1, string path2)
  • [MSBuild]::ValueOrDefault(string value, string default)
  • [MSBuild]::Escape(string unescaped)
  • [MSBuild]::Unescape(string escaped)
  • [MSBuild]::Add(double a, double b)
  • [MSBuild]::Add(long a, long b)
  • [MSBuild]::Subtract(double a, double b)
  • [MSBuild]::Subtract(long a, long b)
  • [MSBuild]::Multiply(double a, double b)
  • [MSBuild]::Multiply(long a, long b)
  • [MSBuild]::Divide(double a, double b)
  • [MSBuild]::Divide(long a, long b)
  • [MSBuild]::Modulo(double a, double b)
  • [MSBuild]::Modulo(long a, long b)
  • [MSBuild]::BitwiseOr(int first, int second)
  • [MSBuild]::BitwiseAnd(int first, int second)
  • [MSBuild]::BitwiseXor(int first, int second)
  • [MSBuild]::BitwiseNot(int first)

The smart thing about those arithmetic methods is that MSBuild converts string values to a matching number type on the fly, so there's no need for any explicit type conversion.

And that would be the whole spectrum of property functions in MSBuild. For further reading, please turn to the official documentation on the MSDN.

Happy coding!

photo credit: Paddington Reservior Gardens Roof via photopin (license)

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About Oliver

shades-of-orange.com code blog logo I build web applications using ASP.NET and have a passion for javascript. Enjoy MVC 4 and Orchard CMS, and I do TDD whenever I can. I like clean code. Love to spend time with my wife and our children. My profile on Stack Exchange, a network of free, community-driven Q&A sites

About Anton

shades-of-orange.com code blog logo I'm a software developer at teamaton. I code in C# and work with MVC, Orchard, SpecFlow, Coypu and NHibernate. I enjoy beach volleyball, board games and Coke.