Get the startup path in a vb.net console exe

后端 未结 3 1839
半阙折子戏
半阙折子戏 2021-01-05 19:06

How do get the startup path ( system.windows.forms.application.StartupPath ) of my exe without adding a reference to system.windows.forms?

相关标签:
3条回答
  • 2021-01-05 19:12

    You could try

    System.AppDomain.CurrentDomain.BaseDirectory
    

    which would work for most cases.

    0 讨论(0)
  • 2021-01-05 19:14

    EDIT: @KiwiBastard's answer is the correct method:

    System.AppDomain.CurrentDomain.BaseDirectory
    

    Add a reference to System.Reflection and use

    Assembly.GetExecutingAssembly().Location
    

    EDIT: Depending where you intend getting the startup path, this might be more appropriate:

    Assembly.GetEntryAssembly().Location
    
    0 讨论(0)
  • 2021-01-05 19:15

    You can get the startup path without reflection by using:

    IO.Path.GetDirectoryName(Diagnostics.Process.GetCurrentProcess().MainModule.FileName)
    
    0 讨论(0)
提交回复
热议问题