Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use the following code to get the version of the application that's running:

C#
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);
               }


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.

Edit: 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.

Edit: I have found that the error is caused by the line
FileVersionInfo myFI = FileVersionInfo.GetVersionInfo("FileName.exe");
                  currentVersion = myFI.FileVersion;


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.
Posted
Updated 21-Oct-11 5:57am
v3
Comments
phil.o 21-Oct-11 10:25am    
Would be great to have the exact error message you are talking about.
Neil Cross 21-Oct-11 10:31am    
I do not know what error is being thrown, though it occurs to me I could output that to a log(slaps head). I'll get back to you on that.
phil.o 21-Oct-11 10:40am    
Nice! This way we would know which line is throwing the exception :)
Neil Cross 21-Oct-11 10:44am    
I have included the error in my question. I know it's not very imforamtive, but that's what's outputted to my textbox, and I can't replicate the error to see it in visual studio.
Ciumac Sergiu 21-Oct-11 11:07am    
The only possible exception that I see in this code is the FileNotFound exception. Have you considered the fact that the FileName.exe is missing on some computers?

1 solution

Aren't you better off with
C#
string versionNumber = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
 
Share this answer
 
Comments
Neil Cross 21-Oct-11 10:31am    
I have tried both.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900