C# - 获取嵌入式资源文件的文件版本

时间:2013-11-27 18:25:23

标签: c# wpf visual-studio-2012

我有几个文件作为WPF应用程序的嵌入资源。我希望能够找到这些资源的文件版本,而无需先将它们写入文件。这可能吗?

1 个答案:

答案 0 :(得分:-1)

我不确定这是否会有所帮助,但在搜索了如何将我的嵌入式资源.DLL打包成一个.exe后,我遇到了下面的代码。您可以使用Assembly来收集嵌入的资源,例如.DLL的文件版本。简而言之,使用Assembly.Load(byte []),您可以找出文件版本。

var assemblies = new Dictionary<string, Assembly>();
var executingAssembly = Assembly.GetExecutingAssembly();
var resources = executingAssembly.GetManifestResourceNames().Where(n => n.EndsWith(".dll"));

foreach (string resource in resources)
{
    using (var stream = executingAssembly.GetManifestResourceStream(resource))
    {
        if (stream == null)
            continue;

        var bytes = new byte[stream.Length];
        stream.Read(bytes, 0, bytes.Length);
        try
        {
            //After Assembly.Load is called, you can find the File Version
            assemblies.Add(resource, Assembly.Load(bytes));
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.Print(string.Format("Failed to load: {0}, Exception: {1}", resource, ex.Message));
        }
    }
}

可以找到一些来源here

This也可能会有所帮助。

相关问题