0

How would automatically update the AssemblyFileVersion (Visual Studio, WPF C# application) to the current date, format YY.MM.DD (23.05.09) using a powershell pre build event.

I've tried this:

Powershell -Command "(Get-Content $(ProjectDir)Properties\AssemblyInfo.cs) | Foreach-Object { $_ -replace 'AssemblyFileVersion\("[\d\.]+"\)', 'AssemblyFileVersion("' + (Get-Date -Format "yy.MM.dd") + '")' } | Set-Content $(ProjectDir)Properties\AssemblyInfo.cs"

But get error:

ObjectNotFound: (C:\...AssemblyInfo.cs:String) [Get-Content], ItemNotFoundEx
 FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

EDIT:

AssemblyInfo.cs was not located in Properties directory so changing it to:

$(ProjectDir)AssemblyInfo.cs)

Fixes the first error but now I get this error:

| Foreach-Object { $_ -replace 'Assemb ...
1>+                                                          ~~~~~~~~
1>    + CategoryInfo          : InvalidOperation: (System.Object[]:Object[]) [], RuntimeException
1>    + FullyQualifiedErrorId : BadReplaceArgument

EDIT2:

Command no error, but still don't update FileVersion.

AssebmlyFileInfo.cs:

[assembly: AssemblyFileVersion("23.05.03")]. 

Command:

Powershell -Command "(Get-Content $(ProjectDir)AssemblyInfo.cs) | Foreach-Object { $_ -replace 'AssemblyFileVersion\("[\d\.]+"\)', ('AssemblyFileVersion("' + (Get-Date -Format "yy.MM.dd") + '")') } | Set-Content $(ProjectDir)AssemblyInfo.cs"
Patrik Fröhler
  • 1,221
  • 1
  • 11
  • 38
  • as advice, I would strongly suggest *not* using the date - as that doesn't give repeatable builds; nerdbank.gitversioning is the approach I see most often – Marc Gravell May 09 '23 at 10:23
  • I'm not curranty using git / source control, just wanted something simple to use until I do it more proper later on,. And thought setting AssemblyFileVersion to the current date would have been simple enough. – Patrik Fröhler May 09 '23 at 10:35
  • 1
    The error message implies that `$(ProjectDir)Properties\AssemblyInfo.cs` didn't expand to an existing file path. – mklement0 May 09 '23 at 13:26
  • Maybe output `$(ProjectDir)`? I'm not sure whether it's available correctly during a pre-build step. – PMF May 09 '23 at 14:01
  • I fixed the error with the path but, get a new different error now (updated the post) – Patrik Fröhler May 09 '23 at 14:27
  • 1
    `'AssemblyFileVersion("' + (Get-Date -Format "yy.MM.dd") + '")'` -> `('AssemblyFileVersion("' + (Get-Date -Format "yy.MM.dd") + '")')` (enclosure in `(...)` required) – mklement0 May 09 '23 at 14:28
  • That fixes the error, but when building it do not update AssemblyFileVersion, AssemblyInfo.cs it still says [assembly: AssemblyFileVersion("23.05.03")] – Patrik Fröhler May 09 '23 at 14:41
  • That implies that your `-replace` operation has no effect, which in turn implies that your regex didn't match. Tweak the PowerShell command in an interactive session first, to make sure that it works. – mklement0 May 09 '23 at 14:42
  • 1
    Your PowerShell `-Command` string has _embedded_ `"` that aren't _escaped_. In the simplest case, escape them as ``\"`` (should be enough here). See [this answer](https://stackoverflow.com/a/49060341/45375) for more information. – mklement0 May 09 '23 at 15:05

1 Answers1

1

Working Command:

Powershell -Command "(Get-Content $(ProjectDir)AssemblyInfo.cs) | Foreach-Object { $_ -replace 'AssemblyFileVersion\(\"[\d\.]+\"\)', ('AssemblyFileVersion(\"' + (Get-Date -Format "yy.MM.dd") + '\")') } | Set-Content $(ProjectDir)AssemblyInfo.cs"
Patrik Fröhler
  • 1,221
  • 1
  • 11
  • 38
  • I'm glad to hear it, but my guess is that this will only be helpful to future readers if you clean up your question to _only_ show what the _ultimate_ problem was - the one that your answer now addresses - and to add some explanation to your answer. – mklement0 May 09 '23 at 15:22