5

I am creating a file using:

File.WriteAllText(FILEPATHNAME, "SOME VALUE");

When creating a file, is it possible to specify a version programmatically? So that if someone were to use FileVersionInfo object they would get the version I specified?

mtijn
  • 3,610
  • 2
  • 33
  • 55
user1202434
  • 2,243
  • 4
  • 36
  • 53
  • 1
    you'll probably end up saving the version inside the file, as a line of text. – Adam Aug 22 '12 at 13:10
  • Hi, this is actually a special file that needs content formatted in a specific way, so i may not be able to have special version info in the text file itself. Thanks for your answer. – user1202434 Aug 22 '12 at 13:15

4 Answers4

4

The FileVersionInfo is a binary resource that can be stored in an executable file. However, you are writing a text file and those files cannot contain binary resources. Asking for the version (as defined by FileVersionInfo) of a text file does make sense

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
3

Unfortunately, it is not possible. Version information is an embedded resource attached to a file and only binary files can have embedded resources.

See the remarks section of FileVersionInfo Class for more information.

As an alternate approach, you can create an additional text file for each text file you are writing containing the version information.

M. Mennan Kara
  • 10,072
  • 2
  • 35
  • 39
2

No, there is no way to write file version in that way, nor that I'm aware of, honestly.

Possible option is:

Write file version into the file itself within some formatted data.

The most common approach I see, is writing into the beginning of the file or on the first line of the file. So you can fast access that information and decide either process with reading or not.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Hi, this is actually a special file that needs content formatted in a specific way, so i may not be able to have special version info in the text file itself. Thanks for your answer. – user1202434 Aug 22 '12 at 13:13
  • 2
    @user1202434: if you can't change the format of the file, there is another option (not sure if it fits your requirements) is creating your format with (say) zipped folder. So you can include a manifest with whatever info you want. Like, for example, MS DOCX format works (it's a simple zipped folder with a lot of files inside). – Tigran Aug 22 '12 at 13:17
  • thanks for the suggestion. Ill have to see if that works for me..let me try. – user1202434 Aug 22 '12 at 13:21
1

If you really really cannot enter the version into the file itself, maybe try putting the version in an NTFS "alternate data stream" (ADS).

You'll need to know what you're doing though, since that has its own gotchas.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • Thank you for the answer...learn something new everyday. Never knew about "ADS" before today. – user1202434 Aug 22 '12 at 13:26
  • @user1202434: Glad you learned something! That's why I mentioned it. :) Btw, that's also how Explorer stores Summary information about some files... they're useful in some cases, but you have to be aware that they might be lost across backups/file copies/etc., and that it won't work if the user is using a different file system. It's usually a risky solution unless you know *exactly* what you're doing... also be aware that .NET may not support this out of the box, I don't know. – user541686 Aug 22 '12 at 13:28