FileVersionInfo GetVersionInfo() returns empty result

1k Views Asked by At

I've written a simple C# program that compares the file version of the local file with that of server and if there is a mismatch it should overwrite the local copy with that of server. Code snippet below.

using System;
using System.Diagnostics;
using System.Security.Principal;

namespace Test
{
    public class FileVersionComparer
    {
        public static void Main()
        {
            // Print the user name
            Console.WriteLine("This app is launched by - {0}",WindowsIdentity.GetCurrent().Name);

            // Get the server file version
            string serverFilePath = @"\\myserver\folder\test.exe";
            FileVersionInfo serverVersionInfo = FileVersionInfo.GetVersionInfo(serverFilePath);
            string serverFileVersion = serverVersionInfo.FileVersion;
            Console.WriteLine("Server file version : {0}", serverFileVersion);

            // Get the local file version
            string localFilePath = @"C:\Windows\test.exe";
            FileVersionInfo localVersionInfo = FileVersionInfo.GetVersionInfo(localFilePath);
            string localFileVersion = localVersionInfo.FileVersion;
            Console.WriteLine("Local file version : {0}", localFileVersion);

            // Compare and overwrite if version is not same
            if(!string.Equals(localFileVersion, serverFileVersion,StringComparison.OrdinalIgnoreCase))
            {
                File.Copy(serverFilePath, localFilePath, true);
            }
            else
                Console.WriteLine("File version is same");

        }
    }
}

This program is launched as a child process and the parent application and hence the child runs under NT AUTHORITY\SYSTEM account. The program is working fine on my machine but failing to retrieve the local file version (returns empty string, no exception) on couple of machines. On the machines where it is returning empty file version, if I run the program from command prompt as a normal user and as an independent process, it is able to retrieve the local file version correctly.

NOTE: It is able to retrieve the server file version even when launched as child process on all machines.

I also tried to copy the local file from C:\Windows to C:\Temp (using the File.Copy() API) to test if the properties can be read when the same file is in a different location. But then the program throws a File Access exception (when run from the parent app). There are no access restrictions on C:\Temp. Also, from technical forums, I understand that NT AUTHORITY\SYSTEM is an Admin account.

I don't understand why the program is not able to retrieve the local file version when it is run under NT AUTHORITY\SYSTEM account and why it is successful when run with my account as a normal process (NOT as a child process).

Can somebody please help? Thanks.

0

There are 0 best solutions below