本文介绍了在C#中的目录路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WinForm应用程序,我需要像

In my winform application i need a path like

"D:\NEWD\WIN_NEW\Sales\Report\Report2.rdlc"

但我只有销售\报告\ Report2.rdlc的事情是DAT我不能predict这条路 D :\ NEWD \ WIN_NEW 但是当我使用

But i have only "Sales\Report\Report2.rdlc" the thing is dat i can't predict this path D:\NEWD\WIN_NEW but when am using

Directory.GetCurrentDirectory() + "\\Sales\\Report\\Report2.rdlc";

路径包括我的斌\调试目录。我怎样才能获得所需的路径?

the path includes my bin\debug directory. How can i get the desired path?

推荐答案

如果你知道你总是要在斌\调试,你可以只是做

If you know that you are always going to be in bin\debug you could just do

 var path=System.IO.Path.GetFullPath(@"..\..\Sales\Report\Report2.rdlc");

或者你可以做一些检测

Or you could do some detection

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);
}

检测的另一种形式是做只有当你正在调试的路径条在这种情况下调试应设置,所以你可以做......

Another form of detection would be to do the path strip only if you are debugging in which case DEBUG should be set so you could do...

var path=@"Sales\Report\Report2.rdlc";
#if DEBUG

path=@"..\..\"+path;

# end if
path=System.IO.Path.GetFullPath(path);

这最后的版本有所有额外的检测没有被编译到您的版本code中的优势。

This last version has the advantage that all the extra detection is not compiled into your release code.

这篇关于在C#中的目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:07