tags:

views:

154

answers:

6

I have an exe file simpleservice.exe in the physical path F:\SAMPLEPRODUCT\Bin ,, i need to fetch version number of that exe file,,Can you give the code required to fetch the version number

+5  A: 

You can use

FileVersionInfo.GetVersionInfo

for this

Eg:

public void GetFileVersion() {
    // Get the file version for the exe.
    FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo("your_exe_file");

    // Print the file name and version number.
    textBox1.Text = "File: " + myFileVersionInfo.FileDescription + '\n' +
       "Version number: " + myFileVersionInfo.FileVersion;
 }
rahul
+1 though won't this only work on the assumption the assembly was generated with fileversion and assemblyversion set to the same thing?
Neil Barnwell
+2  A: 
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(@"F:\SAMPLEPRODUCT\Bin\simpleservice.exe");
Console.WriteLine(fvi.FileVersion);
Thomas Levesque
i put these code in Form1_Load event but nothing showing
peter
i got it ,,this code working fine
peter
Is there any other way for finding the location of a file means physical path,,Means here 'simpleservice.exe' is in these physical path F:\SAMPLEPRODUCT\Binis there any other simple common way for finding path like server.mappath in ASP.NET,because the path will change some times na,if we copy it into differentfile
peter
I don't understand your question... You need to know the file location, if you don't the system can't guess it for you...
Thomas Levesque
+2  A: 

I'd use the following to do this:

Assembly.LoadFrom("...").GetName().Version.ToString();

or I'd use the FileVersionInfo class. Take your pick:

FileVersionInfo.GetVersionInfo("...");
Pete OHanlon
This works only for a .NET assembly, not for any executable
Thomas Levesque
I'd accidentally hit submit and not finished typing in the rest of my comment. I re-edited it to show the bit I missed when I clicked Submit.
Pete OHanlon
here inside Assembly.LoadFrom("...") what we need to give physical path of exe or ?
peter
OK... I would remove the down vote, but there seems to be a bug... "Vote too old to be changed, unless this answer is edited". Sorry :S
Thomas Levesque
No problem Thomas - I'm not answering them for the votes, so I'm not upset. I really need to remap the hotkeys on my keyboard to prevent these accidental submits - it's the third time I've done this in a couple of days.
Pete OHanlon
@peter - you need to give the physical path to the exe here.
Pete OHanlon
+1  A: 
    AssemblyName anm = AssemblyName.GetAssemblyName( 
     "c:\\winnt\\microsoft.net\\framework\\v1.0.3705\\mscorlib.dll");
    // and show its version
    Console.WriteLine(anm.Version.ToString());
ArsenMkrt
This works only for a .NET assembly, not for any executable
Thomas Levesque
yea but he marks the question with winform and C# tag which give me an assumption that he mean .net assembly
ArsenMkrt
+1  A: 

AssemblyName.GetAssemblyName(@"F:\SAMPLEPRODUCT\Bin\simpleservice.exe").Version

Tadas
Is there any other way for finding the location of a file means physical path,,Means here 'simpleservice.exe' is in these physical path F:\SAMPLEPRODUCT\Binis there any other simple common way for finding path like server.mappath in ASP.NET,because the path will change some times na,if we copy it into differentfile
peter
This code is working with One line of code,,more optimized one
peter
+1  A: 
public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }
Mahin