获取版本号错误 [英] Getting version number error
问题描述
我使用以下代码来获取正在运行的应用程序的版本:
I use the following code to get the version of the application that''s running:
try
{
string currentVersion = System.Reflection.Assembly.GetCallingAssembly().GetName().Version.ToString();
FileVersionInfo myFI = FileVersionInfo.GetVersionInfo("FileName.exe");
currentVersion = myFI.FileVersion;
}
catch (Exception ex)
{
setDebugText("Error retrieving version number: " + ex.Message);
}
在大多数情况下,这是可行的,但是在某些计算机上会引发错误,并且我无法在计算机上复制该错误.是什么会导致反射引发错误,以及如何在工作计算机上复制这些条件.任何帮助将不胜感激.
由于某种原因,输出到文本框的错误是Filename.exe.它给我的只是引发错误的可执行文件的名称.
我发现错误是由
Most of the time this works, however on some computers an error is thrown, and I can''t replicate the error on my computer. What can cause reflection to throw an error, and how can I replicate these conditions on my work computer. Any help will be appreciated.
For some reason the error outputted to a textbox is Filename.exe. All it give me is the name of the executable that threw the error.
I have found that the error is caused by the line
FileVersionInfo myFI = FileVersionInfo.GetVersionInfo("FileName.exe");
currentVersion = myFI.FileVersion;
行引起的
我已经对此进行了注释,并使用函数GetExecutingAssembly()而不是GetCallingAssembly,这摆脱了错误.我仍然想知道为什么这条线会在几台计算机上引起问题.谢谢您到目前为止的评论.
I have commented this out and use the function GetExecutingAssembly() instead of GetCallingAssembly and this got rid of the error. I would still like to know why this line would cause problems on a few computers though. Thanks for your comments so far.
推荐答案
使用
会更好吗?
Aren''t you better off with
string versionNumber = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
这篇关于获取版本号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!