How do I get the path of the assembly the code is in?

前端 未结 30 3343
小蘑菇
小蘑菇 2020-11-21 16:17

Is there a way to get the path for the assembly in which the current code resides? I do not want the path of the calling assembly, just the one containing the code.

<
30条回答
  •  佛祖请我去吃肉
    2020-11-21 17:12

    Here is a VB.NET port of John Sibly's code. Visual Basic is not case sensitive, so a couple of his variable names were colliding with type names.

    Public Shared ReadOnly Property AssemblyDirectory() As String
        Get
            Dim codeBase As String = Assembly.GetExecutingAssembly().CodeBase
            Dim uriBuilder As New UriBuilder(codeBase)
            Dim assemblyPath As String = Uri.UnescapeDataString(uriBuilder.Path)
            Return Path.GetDirectoryName(assemblyPath)
        End Get
    End Property
    

提交回复
热议问题