[英]Directory path in C#
在我的winform应用程序中,我需要一条类似
"D:\NEWD\WIN_NEW\Sales\Report\Report2.rdlc"
但是我只有"Sales\\Report\\Report2.rdlc"
,这是事实,我无法预测此路径D:\\NEWD\\WIN_NEW
但是在使用时
Directory.GetCurrentDirectory() + "\\Sales\\Report\\Report2.rdlc";
该路径包括我的bin\\debug
目录。 如何获得所需的路径?
在进行调试时,当前目录的路径(执行程序的路径)将位于。\\ bin \\ debug \\目录中。
当您部署工具时,您将不再有此问题。
看来,您想使用您的工具来部署“ Sales”目录:因此,也许您应该将其包含在解决方案中,然后选择它以使用可执行文件复制到输出目录(。\\ bin \\ debug)。
如果您知道您将永远处于bin \\ debug中,则可以这样做
var path=System.IO.Path.GetFullPath(@"..\..\Sales\Report\Report2.rdlc");
或者您可以进行一些检测
var path=@"Sales\Report\Report2.rdlc";
var currentDir=System.IO.Directory.GetCurrentDirectory();
if (currentDir.ToLower().EndsWith(@"\bin\debug") ||
currentDir.ToLower().EndsWith(@"\bin\release")) {
path=System.IO.Path.GetFullPath(@"..\..\" + path);
} else {
path=System.IO.Path.GetFullPath(path);
}
另一种检测方式是仅在调试时才执行路径剥离,在这种情况下应设置DEBUG以便执行...
var path=@"Sales\Report\Report2.rdlc";
#if DEBUG
path=@"..\..\"+path;
# end if
path=System.IO.Path.GetFullPath(path);
最新版本的优点是,所有额外的检测都不会编译到您的发行代码中。
嗯,我认为这似乎是一个更棘手的问题:考虑到现在您有了bin\\Debug
,在部署时该路径就消失了,所以说GetCurrentDirectory(),我想说最好使用Path.GetDirectoryName(Application.ExecutablePath)
,您将获得您的部署路径。 还有一天,您可能需要更改部署的文件结构,所以...
只需在bin \\ Debug或bin \\ Release中复制将来的部署文件/文件夹布局,因此您的应用程序将以相同的方式在DEBUG和DEPLOY中工作。 总是有可能。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.