简体   繁体   English

如何在WinForm应用程序项目中获取目录的路径

[英]How do I get path of the Directory inside my WinForm app project

I have a directory named reports inside my winform project in .net. 我在.net中的winform项目中有一个名为reports的目录。 My project name is AccountingReports and inside that Directory reports exists. 我的项目名称是AccountingReports,内部存在目录报告。 So i need the way to access this path via code. 所以我需要通过代码访问此路径的方法。 In Asp.net we use Request.PhysicalApplicationPath property. 在Asp.net中,我们使用Request.PhysicalApplicationPath属性。 So is there any method or property exists that will give me the root of my project 那么有没有任何方法或属性可以为我提供项目的根目录

You can use: 您可以使用:

Directory.GetCurrentDirectory

Gets the current working directory of the application. 获取应用程序的当前工作目录。

You can then append "Reports" to that using the following: 然后,您可以使用以下内容将“报告”附加到该报告:

Path.Combine(string, string) : Path.Combine(string, string)

Dim reportsFolder As String
reportsFolder = Path.Combine(Directory.GetCurrentDirectory(), "Reports")
Dim path As String = AppDomain.CurrentDomain.BaseDirectory 'replace \bin\Debug\ the part of the path you dont want with part you do want Dim path1 As String = path.Replace("\bin\Debug\", "\htmlFiles\")

Well. 好。 A lil' bit late maybe, but I just had the same problem and solved it like this: 可能会迟到一点,但我遇到了同样的问题并解决了这个问题:

    Dim Path As String() = Directory.GetCurrentDirectory.ToString.Split(New Char() {"\"c})
    For i = 0 To Path.Count - 3
        PathLb.Text = PathLb.Text & "\" & Path(i)
    Next i
    PathLb.Text = PathLb.Text.Remove(0, 1)

With this the path is contained on 'PathLb'. 有了这个,路径就包含在'PathLb'中。

Hope its useful to anyone. 希望它对任何人都有用。

When running in the IDE vs. Installed I have used: 在IDE和Installed中运行时我使用过:

If System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed - true if installed, false IDE 如果System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed - 如果安装则为true,false IDE

installed - System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory 安装 - System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory

IDE - My.Application.Info.DirectoryPath IDE - My.Application.Info.DirectoryPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM