2

Basically I need a field containing a compile timestamp. How can this be archived?

DateTime compileDateTime = //At Compiletime: DateTime.Now

The field might also be defined as a constant.

It is crucial that the field is defined at compile time. The application is executed on an embedded device in "sandbox mode". There is no access to the file system, so the assembly itself can not be loaded to retrieve the time stamp.

To make it more clear. (Since to many people where focused on retrieving the build date from the assembly) I could also ask to "hardcode" a random number for every build.

int myConstNumberForThisBuild = //At compile-time: new Random().Next()

Stijn Van Antwerpen
  • 1,840
  • 17
  • 42
  • 1
    One easy possibility is pre-build step generating cs-file where this constant is defined. – Sinatr Aug 05 '16 at 12:35
  • I guess you want the actual time at which the application was compiled? – GolezTrol Aug 05 '16 at 12:37
  • @Sinatr or any file. A text file. – rory.ap Aug 05 '16 at 12:37
  • @DavidG, please read the question! This is in no way similar to the duplicate you marked it. I ask about setting a field, not retrieving a date from another assembly. – Stijn Van Antwerpen Aug 05 '16 at 12:40
  • File.GetCreationTime(Assembly.GetExecutingAssembly().Location) – Narek Arzumanyan Aug 05 '16 at 12:41
  • 2
    @ Narek Arzumanyan `GetCreationTime` will give date of 1st compilation not last (file creation not last modified). But `GetLastWriteTime` wil answer the question. – Leonid Malyshev Aug 05 '16 at 12:43
  • @StijnVanAntwerpen -- Unfortunately, you can't ping DavidG like that. He won't get it. There's not way to get his attention. – rory.ap Aug 05 '16 at 12:43
  • I can't read the assembly at run-time (there is no acces to the file system where the application is executed (don't ask :-) )). I really need to set the value at compile (or pre-compile) time. – Stijn Van Antwerpen Aug 05 '16 at 12:44
  • @roryap Yes I will get a ping! – DavidG Aug 05 '16 at 12:44
  • I'm happy to re-open it for a valid reason, not heard one yet. – DavidG Aug 05 '16 at 12:45
  • @Stijn Van Antwerpen looks like you don't understand what you want. A constant (which must be set before compilation) or a value (which will be calculated while running ie after compilation) – Leonid Malyshev Aug 05 '16 at 12:45
  • @DavidG -- Huh. I was studying a meta post recently about who gets what ping. I guess I misunderstood it. I was just going off the fact that it doesn't suggest your name. Of course, it does now that you're commenting. – rory.ap Aug 05 '16 at 12:45
  • @roryap It may be Mjolnir related, don't think I'd get pinged from a normal close vote. – DavidG Aug 05 '16 at 12:46
  • @DavidG -- [Mjolnir](http://marvel.wikia.com/wiki/Mjolnir)? – rory.ap Aug 05 '16 at 12:47
  • 1
    @roryap [Mjolnir](http://meta.stackoverflow.com/questions/268608/where-did-the-use-of-the-name-mjolnir-come-from)! – DavidG Aug 05 '16 at 12:48
  • If you can't read the assembly, then how can you possibly load it? – DavidG Aug 05 '16 at 12:53
  • @DavidG, I don't want to load the assembly. I want to "hardcode" a value that need to be set at compile time. Like `DateTime copileTime = new DateTime(2016,05,08,14,55,...)` but I don't want to change the value by hand every time I rebuild. – Stijn Van Antwerpen Aug 05 '16 at 12:55
  • So you want the compile time of some random assembly in your solution? This really doesn't make sense to me yet. – DavidG Aug 05 '16 at 12:57
  • No, I want the compile time of the assembly itself. – Stijn Van Antwerpen Aug 05 '16 at 13:00
  • That means you have implicit access to the file that contains the assembly, so the linked duplicate will work. – DavidG Aug 05 '16 at 13:01
  • @DavidG No, it will fail on the creation of the FileStream... There is no acces to the file-system. Actually, the FileStream class is even not available in the sandbox. – Stijn Van Antwerpen Aug 05 '16 at 13:06
  • The linked dupe has lots of answers that don't involve reading the file. Try [this](http://stackoverflow.com/a/1601079/1663001) or some variation of [this](http://stackoverflow.com/a/15502932/1663001) for example. – DavidG Aug 05 '16 at 13:10
  • That last one is indeed more the solution I was looking for. Thx! But you really don't expect us to read all replies on a post? Question and excepted answer is already a lot to process when looking trough all possible related questions... – Stijn Van Antwerpen Aug 05 '16 at 13:15
  • 2
    Oh god no, we absolutely DO expect you to read them all, so yeah, please don't be so lazy. – DavidG Aug 05 '16 at 13:18

0 Answers0