C# winform 根目录路径。如何再次!

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/857435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:26:17  来源:igfitidea点击:

Winform root directory path.How to again!

c#winformsdeployment

提问by black sensei

Hello guys first of all apologize for asking such a simple and yet redundant question but it seems my case is kinda bit different where googling failed to provide answers. I have a solution with 2 projects say proj1 and proj2 where proj1 is a winform application and proj2 is a classlibrary application.proj1 is sitting here

大家好,首先为问这样一个简单但多余的问题而道歉,但似乎我的情况有点不同,谷歌搜索未能提供答案。我有一个包含 2 个项目的解决方案,比如 proj1 和 proj2,其中 proj1 是一个 winform 应用程序,proj2 是一个类库应用程序。proj1 就在这里

C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\proj1

C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\proj1

ane the classlibrary is here

ane 类库在这里

C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\proj2

C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\proj2

Now i have a report in a subfolder Report where i have my crystalreport and other stuffs i need to have this root path of proj1

现在我在子文件夹 Report 中有一份报告,其中有我的 Crystalreport 和其他东西,我需要拥有 proj1 的这个根路径

C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\proj1

C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\proj1

so that i append @"Report\myreport.rpt"to it. I believe with that it's supposed to be relative path on the deployed machine.correct me if I'm wrong.the main idea is to have relative path to the Report folder at

所以我附加@"Report\myreport.rpt"到它。我相信它应该是部署机器上的相对路径。如果我错了,请纠正我。主要思想是将报告文件夹的相对路径放在

C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\proj\Report.

C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\proj\Report。

from all i searched online like Environment.CurrentDirectoryor Application.StartupPathor Application.ExecutablePathor System.Reflection.Assembly.GetExecutingAssembly().Locationare just given me either the bin/proj1.exe or the debug folder. i just can't figure out how to do that

从我在网上搜索的所有内容中,如Environment.CurrentDirectoryorApplication.StartupPathApplication.ExecutablePathorSystem.Reflection.Assembly.GetExecutingAssembly().Location只是给了我 bin/proj1.exe 或调试文件夹。我就是不知道怎么做

thank you for reading this.oh yeah! and for your kind answer. PS: using C#2.0

感谢您阅读本文。哦,是的!并为您的友好回答。PS:使用 C#2.0

采纳答案by Daniel Brückner

You just have to set the property "Copy to Output Directory" of the report file to "Copy allways" or "Copy if newer". This will copy the report below the actual output directory during a build and you can access it with Path.Combine(Application.StartupPath, "Reports", "report.rpt").

您只需将报告文件的“复制到输出目录”属性设置为“始终复制”或“如果更新则复制”。这将在构建期间将报告复制到实际输出目录下,您可以使用Path.Combine(Application.StartupPath, "Reports", "report.rpt").

回答by Fredrik M?rk

I think the safest way to find in which folder your assembly is located is this:

我认为查找程序集所在文件夹的最安全方法是:

private static string GetAppFolder()
{
    return new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
}

...and using that function you can get your report path:

...并使用该功能您可以获得您的报告路径:

Path.Combine(GetAppFolder(), @"Report\myreport.rpt")

回答by lc.

You're not going to get the projectfolder from the compiled assembly because (as far as I know) it has no idea where it was compiled from. You are, however, going to get the folder where the assembly is running from, which is the "debug folder" you're getting from Application.ExecutablePath.

您不会从编译的程序集中获取项目文件夹,因为(据我所知)它不知道它是从哪里编译的。但是,您将获得运行程序集的文件夹,也就是您从 获得的“调试文件夹” Application.ExecutablePath

You would be better off to copy the reports into a Reports folder into the bin/debug folder and access them from there (you could use Application.ExecutablePathto get this). Then when you need to move the compiled files, you can move the Reports folder with it.

您最好将报告复制到报告文件夹中的 bin/debug 文件夹中并从那里访问它们(您可以使用它Application.ExecutablePath来获取)。然后当您需要移动编译后的文件时,您可以移动 Reports 文件夹。

When you deploy, you would then have following structure:

部署时,您将拥有以下结构:

C:\Program Files\proj1\proj1.exe
C:\Program Files\proj1\Reports\myreport.rpt

and not the following (which is what you're suggesting?):

而不是以下(这是您的建议?):

C:\Program Files\proj1\proj1.exe
C:\Documents and Settings\myname\My Documents\Visual Studio 2005\Projects\proj1\Reports\myreport.rpt

回答by Tanmay

string path =new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName