Sunday, June 17, 2007

Getting Application's Path (Desktop and Windows Mobile)

Getting the Application's Path is different depending on the platform and application type. Ideally, you would just use the Application.StartupPath, but that only works for Forms apps on the desktop. I have provided this approach and another for Desktop Console App and another one for Windows Mobile.

VB.NET - Forms App
Public Function GetAppPath() As String

return System.Windows.Forms.Application.StartupPath

End Function


VB.NET - Console App
Imports System.Reflection
...

Public Function GetAppPath() As String

Return Path.GetDirectoryName([Assembly].GetEntryAssembly().Location)

End Function

VB.NET - on Smart Device (Windows Mobile) Platform
Imports System.Reflection
...

Public Function GetAppPath() As String

Dim asm As [Assembly] = [Assembly].GetExecutingAssembly()

return System.IO.Path.GetDirectoryName(asm.GetName().CodeBase)

End Function

5 comments:

Anonymous said...

thanks dude

Anonymous said...

Nice code man, thanks a lot

Anonymous said...

Thanks!!

Anonymous said...

Thanks a lot pal, excelent code!!!

Gökhan Yaşar said...

Youre my hero man