4

我编写了一个简单的 C# 程序,它将本地文件的文件版本与服务器的文件版本进行比较,如果不匹配,它应该用服务器的文件版本覆盖本地副本。下面的代码片段。

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");

        }
    }
}

该程序作为子进程和父应用程序启动,因此子进程在 NT AUTHORITY\SYSTEM 帐户下运行。该程序在我的机器上运行良好,但无法在几台机器上检索本地文件版本(返回空字符串,无异常)。在它返回空文件版本的机器上,如果我以普通用户和独立进程的身份从命令提示符运行程序,它能够正确检索本地文件版本。

注意:即使在所有机器上作为子进程启动时,它也能够检索服务器文件版本。

我还尝试将本地文件从 C:\Windows 复制到 C:\Temp(使用 File.Copy() API),以测试当同一文件位于不同位置时是否可以读取属性。但是随后程序会引发文件访问异常(从父应用程序运行时)。C:\Temp 没有访问限制。另外,从技术论坛上,我了解到 NT AUTHORITY\SYSTEM 是一个管理员帐户。

我不明白为什么程序在 NT AUTHORITY\SYSTEM 帐户下运行时无法检索本地文件版本,以及为什么在我的帐户作为正常进程(而不是子进程)运行时它会成功。

有人可以帮忙吗?谢谢。

4

0 回答 0