Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I needed the file version one time, and I ended up writing a custom task to get the FileVersion because I couldn't find anything.</p> <pre><code>namespace GranadaCoder.Framework.CrossDomain.MSBuild.Tasks.IO//.FileVersionTask { using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Globalization; using System.Diagnostics; using System.IO; using System.Text; using System.Security; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; public class FileVersionTask : FileBasedTaskBase { private static readonly string ROOT_DIRECTORY = "myrootdir"; private static readonly string FULL_PATH = "myfullpath"; private static readonly string FILE_NAME = "myfilename"; private static readonly string DIRECTORY = "mydirectory"; private static readonly string EXTENSION = "myextension"; private static readonly string VERSION = "myfileversion"; /// &lt;summary&gt; /// Gets or sets the source files. /// &lt;/summary&gt; /// &lt;value&gt;The source files.&lt;/value&gt; [Required] public string SourceFiles { get; set; } /// &lt;summary&gt; /// Gets the file versions as a Task Output property. /// &lt;/summary&gt; /// &lt;value&gt;The file versions.&lt;/value&gt; [Output] public ITaskItem[] FileVersions { get; private set; } /// &lt;summary&gt; /// Task Entry Point. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; protected override bool AbstractExecute() { InternalExecute(); return !Log.HasLoggedErrors; } /// &lt;summary&gt; /// Internal Execute Wrapper. /// &lt;/summary&gt; private void InternalExecute() { IList&lt;string&gt; files = null; if (String.IsNullOrEmpty(this.SourceFiles)) { Log.LogWarning("No SourceFiles specified"); return; } if (!String.IsNullOrEmpty(this.SourceFiles)) { Console.WriteLine(this.SourceFiles); files = base.ConvertSourceFileStringToList(this.SourceFiles); } //List&lt;string&gt; fileVersions = new List&lt;string&gt;(); ArrayList itemsAsStringArray = new ArrayList(); foreach (string f in files) { FileInfoWrapper fiw = null; fiw = this.DetermineFileVersion(f); IDictionary currentMetaData = new System.Collections.Hashtable(); currentMetaData.Add(ROOT_DIRECTORY, fiw.RootDirectory); currentMetaData.Add(FULL_PATH, fiw.FullPath); currentMetaData.Add(FILE_NAME, fiw.FileName); currentMetaData.Add(DIRECTORY, fiw.Directory); currentMetaData.Add(EXTENSION, fiw.Extension); currentMetaData.Add(VERSION, fiw.Version); itemsAsStringArray.Add(new TaskItem(fiw.Version, currentMetaData)); } this.FileVersions = (ITaskItem[])itemsAsStringArray.ToArray(typeof(ITaskItem)); } /// &lt;summary&gt; /// Determines the file version. /// &lt;/summary&gt; /// &lt;param name="fileName"&gt;Name of the file.&lt;/param&gt; /// &lt;returns&gt;File version or 0.0.0.0 if value cannot be determined&lt;/returns&gt; private FileInfoWrapper DetermineFileVersion(string fileName) { FileInfoWrapper fiw = new FileInfoWrapper(); fiw.Directory = string.Empty; fiw.Extension = string.Empty; fiw.FileName = string.Empty; fiw.FullPath = string.Empty; fiw.RootDirectory = string.Empty; fiw.Version = "0.0.0.0"; try { if (System.IO.File.Exists(fileName)) { fiw.Extension = System.IO.Path.GetExtension(fileName); fiw.FileName = System.IO.Path.GetFileNameWithoutExtension(fileName); fiw.FullPath = fileName;// System.IO.Path.GetFileName(fileName); fiw.RootDirectory = System.IO.Path.GetPathRoot(fileName); //Take the full path and remove the root directory to mimic the DotNet default behavior of '%filename' fiw.Directory = System.IO.Path.GetDirectoryName(fileName).Remove(0, fiw.RootDirectory.Length); FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(fileName); if (null != fvi) { if (null != fvi.FileVersion) { fiw.Version = fvi.FileVersion; } } } } catch (Exception ex) { if (ex is IOException || ex is UnauthorizedAccessException || ex is PathTooLongException || ex is DirectoryNotFoundException || ex is SecurityException) { Log.LogWarning("Error trying to determine file version " + fileName + ". " + ex.Message); } else { Log.LogErrorFromException(ex); throw; } } return fiw; } /// &lt;summary&gt; /// Internal wrapper class to hold file properties of interest. /// &lt;/summary&gt; internal sealed class FileInfoWrapper { public string Directory { get; set; } public string Extension { get; set; } public string FileName { get; set; } public string FullPath { get; set; } public string RootDirectory { get; set; } public string Version { get; set; } } } } </code></pre> <p>.msbuild example</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Project DefaultTargets="AllTargetsWrapper" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt; &lt;UsingTask AssemblyFile="GranadaCoder.Framework.CrossDomain.MSBuild.dll" TaskName="FileVersionTask"/&gt; &lt;Target Name="AllTargetsWrapper"&gt; &lt;CallTarget Targets="FileVersionTask1" /&gt; &lt;CallTarget Targets="FileVersionTask2" /&gt; &lt;/Target&gt; &lt;PropertyGroup&gt; &lt;WorkingCheckout&gt;c:\Program Files\MSBuild&lt;/WorkingCheckout&gt; &lt;/PropertyGroup&gt; &lt;ItemGroup&gt; &lt;MyTask1ExcludeFiles Include="$(WorkingCheckout)\**\*.rtf" /&gt; &lt;MyTask1ExcludeFiles Include="$(WorkingCheckout)\**\*.doc" /&gt; &lt;/ItemGroup&gt; &lt;ItemGroup&gt; &lt;MyTask1IncludeFiles Include="$(WorkingCheckout)\**\*.exe" Exclude="@(MyTask1ExcludeFiles)" /&gt; &lt;/ItemGroup&gt; &lt;Target Name="FileVersionTask1"&gt; &lt;FileVersionTask SourceFiles="@(MyTask1IncludeFiles)" &gt; &lt;Output TaskParameter="FileVersions" ItemName="MyFileVersionItemNames"/&gt; &lt;/FileVersionTask&gt; &lt;Message Text=" MyFileVersionItemNames MetaData "/&gt; &lt;Message Text=" ------------------------------- "/&gt; &lt;Message Text=" "/&gt; &lt;Message Text="directory: "/&gt; &lt;Message Text="@(MyFileVersionItemNames-&gt;'%(mydirectory)')"/&gt; &lt;Message Text=" "/&gt; &lt;Message Text=" "/&gt; &lt;Message Text="extension: "/&gt; &lt;Message Text="@(MyFileVersionItemNames-&gt;'%(myextension)')"/&gt; &lt;Message Text=" "/&gt; &lt;Message Text=" "/&gt; &lt;Message Text="filename: "/&gt; &lt;Message Text="@(MyFileVersionItemNames-&gt;'%(myfilename)')"/&gt; &lt;Message Text=" "/&gt; &lt;Message Text=" "/&gt; &lt;Message Text="fullpath: "/&gt; &lt;Message Text="@(MyFileVersionItemNames-&gt;'%(myfullpath)')"/&gt; &lt;Message Text=" "/&gt; &lt;Message Text=" "/&gt; &lt;Message Text="rootdir: "/&gt; &lt;Message Text="@(MyFileVersionItemNames-&gt;'%(myrootdir)')"/&gt; &lt;Message Text=" "/&gt; &lt;Message Text=" "/&gt; &lt;Message Text="fileversion: "/&gt; &lt;Message Text="@(MyFileVersionItemNames-&gt;'%(myfileversion)')"/&gt; &lt;Message Text=" "/&gt; &lt;Message Text=" "/&gt; &lt;Message Text=" "/&gt; &lt;Message Text=" "/&gt; &lt;Message Text="rootdir + directory + filename + extension: "/&gt; &lt;Message Text="@(MyFileVersionItemNames-&gt;'%(myrootdir)%(mydirectory)%(myfilename)%(myextension)')"/&gt; &lt;Message Text=" "/&gt; &lt;Message Text=" "/&gt; &lt;Message Text="List of files using special characters (carriage return)"/&gt; &lt;Message Text="@(MyFileVersionItemNames-&gt;'&amp;quot;%(myfullpath)&amp;quot;' , '%0D%0A')"/&gt; &lt;Message Text=" "/&gt; &lt;Message Text=" "/&gt; &lt;/Target&gt; &lt;ItemGroup&gt; &lt;MyTask2IncludeFiles Include="c:\windows\notepad.exe" /&gt; &lt;/ItemGroup&gt; &lt;Target Name="FileVersionTask2"&gt; &lt;FileVersionTask SourceFiles="@(MyTask2IncludeFiles)" &gt; &lt;Output TaskParameter="FileVersions" PropertyName="SingleFileFileVersion"/&gt; &lt;/FileVersionTask&gt; &lt;Message Text="SingleFileFileVersion = $(SingleFileFileVersion) "/&gt; &lt;/Target&gt; &lt;/Project&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload