![](/img/trans.png)
[英]How to find Current Time of different culture i.e different timezone in c# asp.net?
[英]How do I find the current time and date at compilation time in .net/C# application?
我想在.net應用程序中包含當前時間和日期,以便將其包含在啟動日志中以向用戶顯示它們具有的版本。 是否有可能在編譯期間檢索當前時間,或者我是否必須獲得可執行文件的創建/修改時間?
例如
歡迎來到ApplicationX。 這是在時間建成日-月-年 。
如果您對構建號使用反射,則可以使用它來確定構建編譯的時間。
程序集的版本信息包含以下四個值:
您可以指定所有值,也可以使用星號(*)接受默認的內部版本號,修訂號或兩者。 內部版本號和修訂版默認基於2000年1月1日。
以下屬性將設置Major和minor,但隨后會增加內部版本號和修訂版。
[assembly: AssemblyVersion("5.129.*")]
然后你可以使用這樣的東西:
public static DateTime CompileTime
{
get
{
System.Version MyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
// MyVersion.Build = days after 2000-01-01
// MyVersion.Revision*2 = seconds after 0-hour (NEVER daylight saving time)
DateTime compileTime = new DateTime(2000, 1, 1).AddDays(MyVersion.Build).AddSeconds(MyVersion.Revision * 2);
return compileTime;
}
}
我知道這樣做的唯一方法有點令人費解 -
您可以擁有一個預構建事件,該事件運行一個小型應用程序,可以動態生成源代碼。 一種簡單的方法是覆蓋一個非常小的文件,該文件包含一個類(或部分類),其中日/月/年硬編碼為字符串常量。
如果將其設置為作為預構建事件運行,它將在每次構建之前重寫該文件。
這種語言沒有內置任何語言。
您可以編寫一個預構建步驟來寫出源文件的當前日期和時間(例如,在字符串文字中,或作為生成DateTime
源代碼),然后將其編譯為構建的一部分。
我建議你讓這個源文件盡可能簡單,只包含這些信息。 或者,它可以編輯現有文件。
有關此示例,請參閱MiscUtil的構建文件,該文件將當前SVN修訂版嵌入到AssemblyFileVersion
屬性中。 構建文件的一些配置位:
<!-- See http://msbuildtasks.tigris.org -->
<Import
Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
<!-- Find out what the latest version is -->
<SvnInfo RepositoryPath="$(SvnUrl)">
<Output TaskParameter="LastChangedRevision" PropertyName="Revision" />
</SvnInfo>
<!-- Update the AssemblyInfo with the revision number -->
<FileUpdate Files="$(OutputDirectory)\MiscUtil\MiscUtil\Properties\AssemblyInfo.cs"
Regex='(\[\s*assembly:\s*AssemblyFileVersion\(\s*"[^\.]+\.[^\.]+)\.([^\.]+)(\.)([^\.]+)("\)\s*\])'
ReplacementText='$1.$2.$(Revision)$5' />
您可以使用PostSharp在構建后立即編織日期。 PostSharp帶有一個輕量級的面向方面的編程庫,但它可以擴展到以各種方式編織你需要的任何東西。 它在IL級別工作,但API從中提取了一些內容。
在C程序的makefile中,通常會看到如下內容:
echo char * gBuildSig ="%DATE% %TIME%"; > BuildTimestamp.c
然后將生成的C源文件編譯到圖像中。 以上適用於Windows,因為%date%和%time%變量在cmd.exe中是已知的,但類似的東西在使用cat的Unix上也可以。
您可以使用C#執行相同的操作。 再一次,這就是你使用makefile時的樣子。 您需要一個類和一個公共靜態屬性。
BuildTimestamp.cs:
echo public static class Build { public static string Timestamp = "%DATE% %TIME%";} > BuildTimestamp.cs
然后對於你正在構建的東西,依賴和刪除:
MyApp.exe: BuildTimestamp.cs MyApp.cs
$(_CSC) /target:exe /debug+ /optimize- /r:System.dll /out:MyApp.exe MyApp.cs BuildTimestamp.cs
-del BuildTimestamp.cs
編譯后務必刪除BuildTimestamp.cs文件; 你不想重復使用它。 然后,在您的應用程序中,只需引用Build.Timestamp。
使用MSBuild或Visual Studio,它更復雜。 我無法獲得%date%或%time%來解決。 那些東西都是偽環境變量,我猜這就是原因。 因此,我使用帶有AlwaysCreate = true的Touch任務,采用間接方式獲取時間戳。 這會創建一個空文件。 下一步將源代碼寫入同一文件,引用文件的時間戳。 一個轉折 - 我不得不逃脫分號。
您的預構建步驟應構建目標“BuildTimestamp”。 並確保將該文件包含在編譯中。 然后在后構建步驟中刪除它。
<ItemGroup>
<StampFile Include="BuildTimestamp.cs"/>
</ItemGroup>
<Target Name="BuildTimestamp"
Outputs="@(StampFile)">
<Message Text="Building timestamp..." />
<Touch
AlwaysCreate="true"
Files="@(StampFile)" />
<WriteLinesToFile
File="@(StampFile)"
Lines='public static class Build { public static string Timestamp = "%(StampFile.CreatedTime)" %3B }'
Overwrite="true" />
</Target>
嗨,我使用以下方法相同...
private DateTime ExecutableInfo()
{
System.IO.FileInfo fi = new System.IO.FileInfo(Application.ExecutablePath.Trim());
try
{
return fi.CreationTime;
}
catch (Exception ex)
{
throw ex;
}
finally
{
fi = null;
}
}
您可以在AssemblyInfo.cs中更新Assembly版本,作為構建的一部分。 然后你可以做這樣的事情
FileVersionInfo lvar = FileVersionInfo.GetVersionInfo(FileName);
FileVersionInfo包含您要查找的信息(構建/版本等)。 看看這是否適合你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.