none
How can I compare FileVersionInfo to determine which file is newer? RRS feed

  • Question

  • I need to compare a user's version of a file to the version on the server, to conditionally return a newer version to them. 

    I have code like this:

        public HttpResponseMessage GetHHSetupUpdate(double clientVersion)
        {            
            var binaryFilePath = HostingEnvironment.MapPath(@"~\App_Data\Platypus.exe");
            double currentVersion = getCurrentVersion(binaryFilePath);
            if (clientVersion >= currentVersion)
            {
                return null;
            }
            var stream = new FileStream(binaryFilePath, FileMode.Open);
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new StreamContent(stream);
            result.Content.Headers.ContentType =
                new MediaTypeHeaderValue("application/octet-stream");
            return result;
        }

        private double getCurrentVersion(string binaryFilepath)
        {
            FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(binaryFilepath);
            return (double)fileVersionInfo.FileVersion;
        }

    ...but the last line in getCurrentVersion() fails because FileVersionInfo.FileVersion is a string, not a double. See this for info on that type: http://msdn.microsoft.com/en-%20us/library/system.diagnostics.fileversioninfo.fileversion%28v=vs.110%29.aspx

    Since that is the case, and FileVersionInfo.FileVersion is encoded this way:

        The first 16 bits are the FileMajorPart number.
        The next 16 bits are the FileMinorPart number.
        The third set of 16 bits are the FileBuildPart number.
        The last 16 bits are the FilePrivatePart number.

    ...what is the accepted method[ology] for comparing two files for generational succession?

    If only the FileMajorPart and FileMinorPart were significant, I could extract those pieces and convert them to a double and compare those, but since this file will use the third set of 16 bits that sort of type conversion won't do.

    Is it a matter of decoding the entire string into its four elements and then checking each pair in turn, a la (pseudocode):

        private bool ServerFileIsNewer(FileVersionInfo clientFile, FileVersionInfo serverFile)
        {
            int FileMajorPartClient = //extract that
            int FileMinorPartClient = //extract that
            int FileBuildPartClient = //extract that
            int FileMajorPartServer = //extract that
            int FileMinorPartServer = //extract that
            int FileBuildPartServer = //extract that
        
            return (FileMajorPartClient <= FileMajorPartServer) && 
                          (FileMinorPartClient <= FileMinorPartServer) && 
                          (FileBuildPartClient < FileBuildPartServer); 
        }

    If so (this is the manner in which to proceed), how do I perform the commented out pseudocode ("//extract that")?

    B. Clay Shannon

    Wednesday, February 19, 2014 8:19 PM

Answers

  • Hello,

        Try this solution,

        
    public static int CompareVersions(String strA, String strB)
        {
            Version vA = new Version(strA.Replace(",", "."));
            Version vB = new Version(strB.Replace(",", "."));
    
            return vA.CompareTo(vB);
        }
    -----------------------------------------
     if the reply help you mark it as your answer.
     Free Managed .NET Word Component(Create, Modify, Convert & Print)
    Thursday, February 20, 2014 1:24 AM
  • [...]

    Is it a matter of decoding the entire string into its four elements and then checking each pair in turn, a la (pseudocode):

        private bool ServerFileIsNewer(FileVersionInfo clientFile, FileVersionInfo serverFile)
        {
            int FileMajorPartClient = //extract that
            int FileMinorPartClient = //extract that
            int FileBuildPartClient = //extract that
            int FileMajorPartServer = //extract that
            int FileMinorPartServer = //extract that
            int FileBuildPartServer = //extract that
        
            return (FileMajorPartClient <= FileMajorPartServer) && 
                          (FileMinorPartClient <= FileMinorPartServer) && 
                          (FileBuildPartClient < FileBuildPartServer); 
        }

    [...]


    All of the parts are already extracted, see for example FileMajorPart. One of the solutions:


    private static bool ServerFileIsNewer( FileVersionInfo clientFile, FileVersionInfo serverFile )
    {
        if( serverFile.FileMajorPart > clientFile.FileMajorPart ) return true;
        if( serverFile.FileMajorPart < clientFile.FileMajorPart ) return false;
    
        if( serverFile.FileMinorPart > clientFile.FileMinorPart ) return true;
        if( serverFile.FileMinorPart < clientFile.FileMinorPart ) return false;
    
        if( serverFile.FileBuildPart > clientFile.FileBuildPart ) return true;
        if( serverFile.FileBuildPart < clientFile.FileBuildPart ) return false;
    
        return serverFile.FilePrivatePart > clientFile.FilePrivatePart;
    }
    

    Thursday, February 20, 2014 7:41 AM

All replies

  • Hello,

        Try this solution,

        
    public static int CompareVersions(String strA, String strB)
        {
            Version vA = new Version(strA.Replace(",", "."));
            Version vB = new Version(strB.Replace(",", "."));
    
            return vA.CompareTo(vB);
        }
    -----------------------------------------
     if the reply help you mark it as your answer.
     Free Managed .NET Word Component(Create, Modify, Convert & Print)
    Thursday, February 20, 2014 1:24 AM
  • [...]

    Is it a matter of decoding the entire string into its four elements and then checking each pair in turn, a la (pseudocode):

        private bool ServerFileIsNewer(FileVersionInfo clientFile, FileVersionInfo serverFile)
        {
            int FileMajorPartClient = //extract that
            int FileMinorPartClient = //extract that
            int FileBuildPartClient = //extract that
            int FileMajorPartServer = //extract that
            int FileMinorPartServer = //extract that
            int FileBuildPartServer = //extract that
        
            return (FileMajorPartClient <= FileMajorPartServer) && 
                          (FileMinorPartClient <= FileMinorPartServer) && 
                          (FileBuildPartClient < FileBuildPartServer); 
        }

    [...]


    All of the parts are already extracted, see for example FileMajorPart. One of the solutions:


    private static bool ServerFileIsNewer( FileVersionInfo clientFile, FileVersionInfo serverFile )
    {
        if( serverFile.FileMajorPart > clientFile.FileMajorPart ) return true;
        if( serverFile.FileMajorPart < clientFile.FileMajorPart ) return false;
    
        if( serverFile.FileMinorPart > clientFile.FileMinorPart ) return true;
        if( serverFile.FileMinorPart < clientFile.FileMinorPart ) return false;
    
        if( serverFile.FileBuildPart > clientFile.FileBuildPart ) return true;
        if( serverFile.FileBuildPart < clientFile.FileBuildPart ) return false;
    
        return serverFile.FilePrivatePart > clientFile.FilePrivatePart;
    }
    

    Thursday, February 20, 2014 7:41 AM