代码之家  ›  专栏  ›  技术社区  ›  Anthony Atmaram

在编译时确定值?

c#
  •  0
  • Anthony Atmaram  · 技术社区  · 14 年前

    我们的管理人员希望在显示应用程序生成日期的图标上显示工具提示。我希望有一个在构建时被分配了正确值的变量,这样我就不必每次编译时都更改它。有没有办法在c/.net中做到这一点?

    5 回复  |  直到 14 年前
        1
  •  3
  •   Community CDub    7 年前

    尝试以下代码行

    DateTime buildDate = 
       new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime;
    

    相对回答: How to put the build date of application somewhere in the application?

        2
  •  1
  •   EMP    14 年前

    回答一般问题:不,除了对数字类型和字符串连接进行简单的算术运算外,没有任何方法可以让C编译器在编译时执行任何工作。您不能让它嵌入当前日期。

    但是,对于这个特定的目的,您可以在Jeff Atwood的博客帖子中使用“第三种方式”: http://www.codinghorror.com/blog/2005/04/determining-build-date-the-hard-way.html

        3
  •  1
  •   PjL    14 年前

    最简单的方法是从.exe或.dll本身检查文件日期/时间:

    Assembly asm = Assembly.GetExecutingAssembly();
    if( asm != null && !string.IsNullOrEmpty(asm.Location) ) {
       FileInfo info = new FileInfo(asm.Location);
       DateTime date = info.CreationTime;
    }
    
        4
  •  0
  •   Itay Karo    14 年前

    如何编译解决方案?
    如果是Visual Studio的常规编译,则可以设置预生成事件以运行脚本并更改包含日期的代码文件。

        5
  •  0
  •   codymanix    14 年前

    您可以制作一个工具,它在构建时被调用,并将当前时间作为常量插入到类似assemblyinfo.cs的代码文件中。

    另一种方法是从可执行文件读取文件修改时间。