LegalColyRight在FileVersionInfo C#中始终为空?

LegalColyRight在FileVersionInfo C#中始终为空?,c#,fileversioninfo,C#,Fileversioninfo,我在windows中有一个SFX(自解压可执行文件)文件(使用zip工具创建,如7z,WinRar,…),详细信息如下: 我想获得C#中的版权文本,因此我编写了以下代码: var fileVersionInfo = FileVersionInfo.GetVersionInfo(filePath); Console.Write(fileVersionInfo.LegalCopyright) fileVersionInfo.legalcyright始终为空! 有什么问题吗 编辑:我的原始代码:

我在windows中有一个SFX(自解压可执行文件)文件(使用zip工具创建,如
7z
WinRar
,…),详细信息如下:

我想获得C#中的
版权
文本,因此我编写了以下代码:

var fileVersionInfo = FileVersionInfo.GetVersionInfo(filePath);
Console.Write(fileVersionInfo.LegalCopyright)
fileVersionInfo.legalcyright
始终为空! 有什么问题吗

编辑:
我的原始代码:

var fileVersionInfo = FileVersionInfo.GetVersionInfo(filePath1);
var properties = typeof(FileVersionInfo).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var propertyInfo in properties)
{
    var value = propertyInfo.GetValue(fileVersionInfo);
    Console.WriteLine("{0} = {1}", propertyInfo.Name, value);
}
Console.ReadKey();
结果是:

(我的声誉太低,无法发表评论,所以我把它贴在这里)

我刚刚测试了以下代码,它对我来说工作正常

var fileVersionInfo = FileVersionInfo.GetVersionInfo(@"C:\Users\usr\Desktop\Game\steamIntegration\steam_api.dll");
Console.Write(fileVersionInfo.LegalCopyright);
Console.ReadLine();
可能您的权限不足以访问该文件。将*.manifest添加到项目中,将requestedExecutionLevel更改为:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />


这可能解决了您的问题。

您观察到的行为是由于Microsoft.NET framework类
FileVersionInfo
第411行中的私有函数
GetVersionInfo-ForCodePage
的实现存在缺陷,该函数当前处于版本中,可能更早:

// fileVersion is chosen based on best guess. Other fields can be used if appropriate. 
return (fileVersion != string.Empty);
引用参考源(注释他们的)表示,如果函数的
fileVersion
成员为空(块的成员为空,而不是标头中的成员为空,则函数将放弃以其他方式正确猜测的特定于代码页的版本信息,这会增加混淆)

exe文件就是这种情况:

当我们修补框架以使用它时

return (productVersion != string.Empty);
…它按预期工作(在控制台和Windows应用程序中进行了测试):

因此,有两种选择:

  • 编译exe,使其文件版本不会为空。希望这不是你的压缩工具的错误,没有传输这些信息
  • 向Microsoft提交一个bug。我从他们的参考源许可证中了解到,它不允许在任何产品中包含衍生作品

  • 最后我可以找到一个解决方案:
    1。首先安装以下软件包:

    Microsoft.WindowsAPICodePack.Shell
    
    它有一个依赖项包,Nuget会自动安装它
    Microsoft.WindowsAPICodePack.Core

    2。现在我们可以按照以下代码获取文件属性

    using System;
    using System.Reflection;
    using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
    
    namespace ConsoleApplication1
    {
        internal class Program
        {
            private static void Main()
            {
                const string filePath1 = @"C:\Users\Mohammad\Downloads\Test\.....exe";
                var shellFile = Microsoft.WindowsAPICodePack.Shell.ShellObject.FromParsingName(filePath1);
                foreach (var propertyInfo in typeof(ShellProperties.PropertySystem).GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    var shellProperty = propertyInfo.GetValue(shellFile.Properties.System, null) as IShellProperty;
                    if (shellProperty?.ValueAsObject == null) continue;
                    var shellPropertyValues = shellProperty.ValueAsObject as object[];
                    if (shellPropertyValues != null && shellPropertyValues.Length > 0)
                    {
                        foreach (var shellPropertyValue in shellPropertyValues)
                            Console.WriteLine("{0} = {1}", propertyInfo.Name, shellPropertyValue);
                    }
                    else
                        Console.WriteLine("{0} = {1}", propertyInfo.Name, shellProperty.ValueAsObject);
                }
                Console.ReadKey();
            }
        }
    }
    

    这与其说是一个评论,不如说是一个答案,所以别担心。也许它与SFX文件类型有关,您是仅使用DLL进行测试,还是也使用SFX进行测试?也许shell可以窥探压缩格式,而FileVersionInfo不能?好的,我刚刚下载了7-ZIP SFX Maker v3.3,自己创建了一个SFX。我仍然能够阅读版权,即使有调用器权限。谢谢你的回答,我测试了它,没有变化,
    legalcyright
    仍然是空的。你能告诉我你是如何或用什么程序创建你的SFX应用程序的吗?您是否已经使用管理员权限对其进行了测试?我使用admin对其进行了测试,没有更改,使用7zip 9.22创建的文件PasswordFileVersionInfo没有bug。复习。在这里,您可能犯的最明显的错误是试图获取SFX文件的信息,而不是您自己的文件。或者反过来。添加一个
    控制台。写入(文件路径)
    以证明您正在读取正确的文件。其他版本信息属性也是空的吗?我非常确定
    filePath
    。请参阅编辑部分。第二个屏幕截图显示该文件位于“下载”文件夹中。文件是否被阻止(文件>属性>取消阻止或类似,取决于Windows版本)?@dlatikay:不,它不是阻止。您可以从这里下载并测试它