【问题标题】:information from exe file来自exe文件的信息
【发布时间】:2009-09-07 08:23:12
【问题描述】:

我在物理路径 F:\SAMPLEPRODUCT\Bin 中有一个 exe 文件 simpleservice.exe,我需要获取该 exe 文件的版本号,你能给出获取版本号所需的代码

【问题讨论】:

标签: c# winforms


【解决方案1】:

你可以使用

FileVersionInfo.GetVersionInfo

为此

例如:

public void GetFileVersion() {
    // Get the file version for the exe.
    FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo("your_exe_file");

    // Print the file name and version number.
    textBox1.Text = "File: " + myFileVersionInfo.FileDescription + '\n' +
       "Version number: " + myFileVersionInfo.FileVersion;
 }

【讨论】:

  • +1 虽然这仅在假设程序集是在 fileversion 和 assemblyversion 设置为相同的情况下生成的假设下才有效吗?
【解决方案2】:

我会使用以下方法来做到这一点:

Assembly.LoadFrom("...").GetName().Version.ToString(); 

或者我会使用 FileVersionInfo 类。任君挑选:

FileVersionInfo.GetVersionInfo("...");

【讨论】:

  • 我不小心点击了提交,但没有完成我的其余评论的输入。我重新编辑它以显示我单击提交时错过的部分。
  • 在 Assembly.LoadFrom("...") 里面我们需要给 exe 的物理路径还是?
  • 好的...我会删除反对票,但似乎有一个错误...“投票太旧而无法更改,除非编辑此答案”。对不起:S
  • 没问题 Thomas - 我不会回答他们的选票,所以我并不难过。我真的需要重新映射键盘上的热键以防止这些意外提交 - 这是我几天内第三次这样做。
  • @peter - 您需要在此处提供 exe 的物理路径。
【解决方案3】:
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(@"F:\SAMPLEPRODUCT\Bin\simpleservice.exe");
Console.WriteLine(fvi.FileVersion);

【讨论】:

  • 有没有其他方法可以找到文件的位置是指物理路径,这里的意思是“simpleservice.exe”在这些物理路径中 F:\SAMPLEPRODUCT\Bin 有没有其他简单的常用方法用于在 ASP.NET 中查找 server.mappath 之类的路径,因为路径有时会更改,如果我们将其复制到不同的文件中
  • 我不明白你的问题......你需要知道文件位置,如果你不知道,系统无法为你猜到......
【解决方案4】:
    AssemblyName anm = AssemblyName.GetAssemblyName( 
     "c:\\winnt\\microsoft.net\\framework\\v1.0.3705\\mscorlib.dll");
    // and show its version
    Console.WriteLine(anm.Version.ToString());

【讨论】:

  • 是的,但他用 winform 和 C# 标签标记了这个问题,这让我假设他的意思是 .net 程序集
【解决方案5】:
AssemblyName.GetAssemblyName(@"F:\SAMPLEPRODUCT\Bin\simpleservice.exe").Version

【讨论】:

  • 有没有其他方法可以找到文件的位置是指物理路径,这里的意思是“simpleservice.exe”在这些物理路径中 F:\SAMPLEPRODUCT\Bin 有没有其他简单的常用方法用于在 ASP.NET 中查找 server.mappath 之类的路径,因为路径有时会更改,如果我们将其复制到不同的文件中
  • 此代码使用一行代码,更优化的一行
【解决方案6】:
public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

【讨论】:

    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    相关资源
    最近更新 更多