Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all
I want to get the filename whenever it change(to make my application copy itself).
Posted
Updated 27-May-12 8:57am
v2

Try:
VB
Dim fileName As String = Application.ExecutablePath
 
Share this answer
 
Comments
ahmedtb05 27-May-12 15:13pm    
Thanks.
Monjurul Habib 27-May-12 18:52pm    
5!
In addition to the good answer by Mika: when you got the instance of an assembly, use the property Assembly.Location; it will give you the file name of the main executable of the give assembly:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.location.aspx[^].

I would also note that this is a good method free from problems other method have; for example Application.ExecutablePath is only good for Forms applications; some other methods depends on how the application is hosted and give wrong or useless results for Windows Services or in case of application hosted by the IDE.

—SA
 
Share this answer
 
v3
Comments
Maciej Los 28-May-12 18:12pm    
Good answer, my 5!
Sergey Alexandrovich Kryukov 28-May-12 19:09pm    
Thank you, Maciej.
--SA
Wendelius 1-Jun-12 0:41am    
Good answer
Sergey Alexandrovich Kryukov 1-Jun-12 10:21am    
Thank you, Mika.
--SA
One way to find out the assembly name is to use Assembly.GetExecutingAssembly[^] or Assembly.GetEntryAssembly[^] depending if you want to know the exe or the executing assembly.
 
Share this answer
 
v2
Comments
Monjurul Habib 27-May-12 18:52pm    
5!
Sergey Alexandrovich Kryukov 28-May-12 1:44am    
Really good but just bit incomplete answer, so OP could fail to finally get it. So, I voted 4 this time and added the information on the missing step in my answer; please see.
--SA
Wendelius 1-Jun-12 0:41am    
Thanks :)
Maciej Los 28-May-12 18:14pm    
Good answer, but SA's comment (and answer) is necessary. My 5!
Wendelius 1-Jun-12 0:41am    
Thanks :) Yes it's a good addition.

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