How to define current system date in post build event

帅比萌擦擦* 提交于 2020-12-09 07:15:08

问题


I want to copy .dlls into a target directory of format "test\Project_yyyy.mm.dd" Where yyyy - cuurent year, mm - current month, dd - current date. When I give the below command in my post build event, I am getting an error.

xcopy /Y "$(TargetDir)*.*" test\Project_$(Year:yyyy).$(Month).$(DayOfMonth)

I could not find any macro which define current date. Can someone please help me on this.


回答1:


You can use MSBuild Property Functions to call some of the .Net API. You can use System.DateTime.Now to obtain current time, then fetch year/month/day as properties of this object.

Here is a code that does this:

<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <CurrentYear>$([System.DateTime]::Now.Year)</CurrentYear>
    <CurrentMonth>$([System.DateTime]::Now.Month)</CurrentMonth>
    <CurrentDay>$([System.DateTime]::Now.Day)</CurrentDay>
  </PropertyGroup>

  <Target Name="Print">
    <Message Text="Year: $(CurrentYear)" />
    <Message Text="Month: $(CurrentMonth)" />
    <Message Text="Day: $(CurrentDay)" />
  </Target>
</Project>


来源:https://stackoverflow.com/questions/25484523/how-to-define-current-system-date-in-post-build-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!