Vb.net My.Application.Info.DirectoryPath和Application.StartupPath之间有什么区别

Vb.net My.Application.Info.DirectoryPath和Application.StartupPath之间有什么区别,vb.net,Vb.net,问题真的说明了一切。有什么区别?我想得到应用程序的安装路径,到目前为止还没有发现两者之间的差异 我在MSDN页面中看到的唯一区别是应用程序。StartupPath提到ClickOnce应用程序(我没有运行ClickOnce应用程序-无法忍受!) 也许还有其他方法可以获得它,通过智能感知。这只是“剥猫皮的方法不止一种”的情况,还是每种方法都有优点和缺点?包含在My命名空间中的类型包含在Microsoft.VisualBasic.dll中-它们在其他.NET语言中不常用(或从未使用过!)。应用程序命

问题真的说明了一切。有什么区别?我想得到应用程序的安装路径,到目前为止还没有发现两者之间的差异

我在MSDN页面中看到的唯一区别是
应用程序。StartupPath
提到ClickOnce应用程序(我没有运行ClickOnce应用程序-无法忍受!)


也许还有其他方法可以获得它,通过智能感知。这只是“剥猫皮的方法不止一种”的情况,还是每种方法都有优点和缺点?

包含在
My
命名空间中的类型包含在Microsoft.VisualBasic.dll中-它们在其他.NET语言中不常用(或从未使用过!)。
应用程序
命名空间中的是

在引擎盖下,
Application.StartupPath
执行以下操作:

Public ReadOnly Shared Property StartupPath As String
    Get
        If (Application.startupPath Is Nothing) Then
            Dim stringBuilder As System.Text.StringBuilder = New System.Text.StringBuilder(260)
            UnsafeNativeMethods.GetModuleFileName(NativeMethods.NullHandleRef, stringBuilder, stringBuilder.Capacity)
            Application.startupPath = Path.GetDirectoryName(stringBuilder.ToString())
        End If
        (New FileIOPermission(FileIOPermissionAccess.PathDiscovery, Application.startupPath)).Demand()
        Return Application.startupPath
    End Get
End Property
Public ReadOnly Property DirectoryPath As String
    Get
        Return Path.GetDirectoryName(Me.m_Assembly.Location)
    End Get
End Property
My.Application.Info.DirectoryPath
执行以下操作:

Public ReadOnly Shared Property StartupPath As String
    Get
        If (Application.startupPath Is Nothing) Then
            Dim stringBuilder As System.Text.StringBuilder = New System.Text.StringBuilder(260)
            UnsafeNativeMethods.GetModuleFileName(NativeMethods.NullHandleRef, stringBuilder, stringBuilder.Capacity)
            Application.startupPath = Path.GetDirectoryName(stringBuilder.ToString())
        End If
        (New FileIOPermission(FileIOPermissionAccess.PathDiscovery, Application.startupPath)).Demand()
        Return Application.startupPath
    End Get
End Property
Public ReadOnly Property DirectoryPath As String
    Get
        Return Path.GetDirectoryName(Me.m_Assembly.Location)
    End Get
End Property
这就叫:

Public Overrides ReadOnly Property Location As String
    <SecuritySafeCritical>
    Get
        Dim str As String = Nothing
        RuntimeAssembly.GetLocation(Me.GetNativeHandle(), JitHelpers.GetStringHandleOnStack(str))
         If (str IsNot Nothing) Then
            (New FileIOPermission(FileIOPermissionAccess.PathDiscovery, str)).Demand()
        End If
        Return str
    End Get
End Property
Public将只读属性位置重写为字符串
得到
Dim str As String=Nothing
RuntimeAssembly.GetLocation(Me.GetNativeHandle(),JitHelpers.GetStringHandleOnStack(str))
如果(str不是什么)那么
(新建FileIOPermission(FileIOPermissionAccess.PathDiscovery,str)).Demand()
如果结束
返回str
结束
端属性
StartupPath
中使用的
GetModuleFileName
是对的调用,而
DirectoryPath
中使用的
GetLocation
涉及到一个,因此您需要更深入地挖掘,以找出它的信息来源

TL;DR


首选使用
Application.StartupPath
,以帮助养成良好习惯,因为它不依赖于.NET中添加的
Microsoft.VisualBasic
,如果您选择使用其他语言,它将使您更容易过渡到其他语言。

解释得很好,谢谢。快速侧边栏-如果我正在使用应用程序中已经存在的
My.[Anything]
(我在本例中),我假设也需要这些相同的添加,这意味着在本例中它没有任何区别。@bmgh1985否-一旦加载程序集,没有任何缺点,您可以随意使用它。以同样的方式,我个人也会避免使用
CInt
等,而是使用
Convert.ToInt32
等,更多的是从习惯尽可能使用较新的.NET结构的角度出发,而不是从.NET以前的VB.Awesome遗留下来的问题。感谢您的帮助。My命名空间是在.NET2.0/VS2005中添加的,以帮助VB.NET程序员落入成功的陷阱。两者之间的区别并不微妙,App.SP会向操作系统询问启动进程的EXE。在很多情况下,如COM服务器、Office加载项、ClickOnce,这些都不是您的。