[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

get .exe version property from other host in network using asp.net

Hi,
using asp.net application I copy .exe file from one host to other in same domain. After I finish copy action I must check versio on host that .exe file was copied. Both paths to exe file are fixed and not changing in any case. Version checking action should not generate any extra network traffic (like that application should go to host and copy .exe file to server where app is published and then get version property I dont need that).

This is in short what I need, check version of .exe file from one of the hosts in domain and present it in my asp.net application without any additional network traffic.

Thanks for any suggestion and eventual solution.
0
kahvedzic
Asked:
kahvedzic
  • 4
  • 3
  • 2
  • +2
2 Solutions
 
Praveen VenuCommented:
0
 
kahvedzicAuthor Commented:
First, this way I can only check applications created in Visual Studio, but I also copy .exe files of VB6 compiled apps that havent got any assembly version value.
Other problem with this solution is that for .exe file (size 555 Kb) application generate 365 Kb network traffic, compare to this same action in vb5 application generate 223 Kb of network traffic.
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
Try
var info = FileVersionInfo.GetVersionInfo("\\your\path\to\yourexe.exe");
info.FileVersion

http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.getversioninfo.aspx

Altyernatively, if you have the code in a Vb application, why not re-use that by compiling it into a COM library and using COM Interop to access the component from ASP.NET?

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Carlos VillegasFull Stack .NET DeveloperCommented:
@ToAoM I was going to say that! ;)
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
just referring to the FileVersionInfo class ok...
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
:D
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hi kahvedzic, if the network traffic is a concern for you, you can try this:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplicationFileVersion
{
    class Program
    {
        [DllImport("version.dll", CharSet = CharSet.Auto)]
        private static extern bool GetFileVersionInfo(string lptstrFilename, int dwHandle, int dwLen, HandleRef lpData);

        [DllImport("version.dll", CharSet = CharSet.Auto)]
        private static extern bool VerQueryValue(HandleRef pBlock, string lpSubBlock, [In, Out] ref IntPtr lplpBuffer, out int len);

        static void Main(string[] args)
        {
            Console.Write("File Path: ");
            string version = string.Empty;
            string fileName = Console.ReadLine();
            if (string.IsNullOrEmpty(fileName))
                return;

            unsafe
            {
                int fileVersionInfoSize = 32000;
                byte[] buffer = new byte[fileVersionInfoSize];
                fixed (byte* numRef = buffer)
                {
                    IntPtr handle = new IntPtr((void*)numRef);

                    GetFileVersionInfo(fileName, 0, fileVersionInfoSize, new HandleRef(null, handle));
                    int varEntry = GetVarEntry(handle);
                    string codepage = ConvertTo8DigitHex(varEntry);
                    version = GetFileVersionString(handle, @"\\StringFileInfo\\" + codepage + "\\FileVersion");
                }
            }

            Console.WriteLine("FileVersion: " + version);
            Console.ReadKey();
        }

        private static int GetVarEntry(IntPtr memPtr)
        {
            int num;
            IntPtr zero = IntPtr.Zero;
            if (VerQueryValue(new HandleRef(null, memPtr), @"\VarFileInfo\Translation", ref zero, out num))
            {
                return ((Marshal.ReadInt16(zero) << 0x10) + Marshal.ReadInt16((IntPtr)(((long)zero) + 2L)));
            }
            return 0x40904e4;
        }

        private static string GetFileVersionString(IntPtr memPtr, string name)
        {
            int num;
            string str = "";
            IntPtr zero = IntPtr.Zero;
            if (VerQueryValue(new HandleRef(null, memPtr), name, ref zero, out num) && (zero != IntPtr.Zero))
            {
                str = Marshal.PtrToStringAuto(zero);
            }
            return str;
        }

        private static string ConvertTo8DigitHex(int value)
        {
            string str = Convert.ToString(value, 0x10).ToUpper(System.Globalization.CultureInfo.InvariantCulture);
            if (str.Length == 8)
            {
                return str;
            }
            StringBuilder builder = new StringBuilder(8);
            for (int i = str.Length; i < 8; i++)
            {
                builder.Append("0");
            }
            builder.Append(str);
            return builder.ToString();
        }
    }
}

Open in new window


This example returns the FileVersion attribute value of the file.

I extracted that from FileVersionInfo.GetVersionInfo method implementation, that will use about 100Kb of network traffic, you will need to enable unsafe code in the project build settings to be able to use this, so you can build another class library project with this code and reference it from your main application.

Because I modified this implementation a little you must test it very well (I removed the call to GetFileVersionInfoSize method and I fixed the buffer size to 32000).
0
 
VoloxCommented:
What platforms are the ASP.Net application and the target you are copying to?  If they both have Powershell available then you could use a remote Powershell session to check the file version.  

It's hard to post example code from my phone but if you'd like to pursue and need help with details, let me know.
0
 
VoloxCommented:
What platforms are the ASP.Net application and the target you are copying to?  If they both have Powershell available then you could use a remote Powershell session to check the file version.  

It's hard to post example code from my phone but if you'd like to pursue and need help with details, let me know.
0
 
kahvedzicAuthor Commented:
I did try @ToAoM solution, and when I try to check file version for .exe that is 2.5 MB, only 11Kb of network traffic was generated.
For now this is OK solution. I just need to test how will this work if I try to check file version on multiple locations in one loop, or multiple .exe file version on one location.

@yv989c I will try this solution also.

@Volox Powershell is not an option but thanks for suggestion.
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
I expect it will result in similar amounts of traffic for each file being checked. It's probably also the best you can expect, to extract the file version from a PE file, the header information is extracted and the version if taken from there.

Some files might cause larger amounts of traffic depending on the number of resources included in the file, signatures, and other stuff that could cause the header to grow.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
I confirm the ToAoM statement, that depends of the file structure, I did my test on an exe (self extracting zip) file with 74MB of data aprox, extract that metadata is not as easy as it seems, for more info you can read this post:
http://nunobrito1981.blogspot.com/2010/07/reading-file-version-from-windows-exe.html
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now