FileVersionInfo GetVersionInfo () returns empty result


I wrote a simple program in C # that compares the file version of the local file with the file version with the server, and if there is a mismatch, it should overwrite the local copy with the local server version. Below is a code snippet.

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 runs as a child process and a parent application, and hence the child runs under the NT AUTHORITY \ SYSTEM account. The program works fine on my machine, but can't get the local version of the file (returns an empty string, no exception) on a couple of machines. On machines where it returns an empty version of the file, if I run the program from the command line as a normal user and as an independent process, it can get the local version of the file correctly.

NOTE. It can retrieve the server file version even when running as a child process on all machines.

I also tried copying a local file from C: \ Windows to C: \ Temp (using the File.Copy () API) to check 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 application). There are no access restrictions on C: \ Temp. Also, I understand from the technical forums that NT AUTHORITY \ SYSTEM is an Administrator account.

I do not understand why the program cannot get the local version of the file when it is running in the NT AUTHORITY \ SYSTEM account, and why it is successful when running my account as a normal process (NOT as a child process).

Can anyone help? Thank.

+3


source to share





All Articles