Executable directory where application is running from?

前端 未结 6 1742
无人共我
无人共我 2020-12-01 04:46

I need to get the path (not the executable) where my application is running from:

System.AppDomain.CurrentDomain.BaseDirectory()

When I run

相关标签:
6条回答
  • 2020-12-01 05:04

    You could use the static StartupPath property of the Application class.

    0 讨论(0)
  • 2020-12-01 05:06

    This is the first post on google so I thought I'd post different ways that are available and how they compare. Unfortunately I can't figure out how to create a table here, so it's an image. The code for each is below the image using fully qualified names.

    My.Application.Info.DirectoryPath
    
    Environment.CurrentDirectory
    
    System.Windows.Forms.Application.StartupPath
    
    AppDomain.CurrentDomain.BaseDirectory
    
    System.Reflection.Assembly.GetExecutingAssembly.Location
    
    System.Reflection.Assembly.GetExecutingAssembly.CodeBase
    
    New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase)
    
    Path.GetDirectoryName(Uri.UnescapeDataString((New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase).Path)))
    
    Uri.UnescapeDataString((New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase).Path))
    
    0 讨论(0)
  • 2020-12-01 05:08

    You can write the following:

    Path.Combine(Path.GetParentDirectory(GetType(MyClass).Assembly.Location), "Images\image.jpg")
    
    0 讨论(0)
  • 2020-12-01 05:09
    Dim P As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
    P = New Uri(P).LocalPath
    
    0 讨论(0)
  • 2020-12-01 05:13

    I needed to know this and came here, before I remembered the Environment class.

    In case anyone else had this issue, just use this: Environment.CurrentDirectory.

    Example:

    Dim dataDirectory As String = String.Format("{0}\Data\", Environment.CurrentDirectory)
    

    When run from Visual Studio in debug mode yeilds:

    C:\Development\solution folder\application folder\bin\debug
    

    This is the exact behaviour I needed, and its simple and straightforward enough.

    0 讨论(0)
  • 2020-12-01 05:28
    Dim strPath As String = System.IO.Path.GetDirectoryName( _
        System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
    

    Taken from HOW TO: Determine the Executing Application's Path (MSDN)

    0 讨论(0)
提交回复
热议问题