2011-08-24 209 views
1

在我的WinForm應用程序,我需要像目錄路徑

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

的路徑,但我只有"Sales\Report\Report2.rdlc"事情是DAT我無法預測此路徑D:\NEWD\WIN_NEW但是當我使用

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

該路徑包含我的bin\debug目錄。我如何獲得所需的路徑?

+0

你需要提供更多的信息,沒有辦法猜測任何可能是任何東西的路徑 –

+0

很難理解這個問題。當您將程序移動到目錄D:\ NEWD \ WIN_NEW時,它將起作用。如果你不這樣做,爲什麼你不能只使用「D:\ NEWD \ WIN_NEW \ Sales \ Report \ Report2.rdlc」作爲路徑? – PythEch

回答

4

如果你知道你總是要在斌\調試,你可以只是做

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

或者你可以做一些檢測

var [email protected]"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 [email protected]"Sales\Report\Report2.rdlc"; 
#if DEBUG 

[email protected]"..\..\"+path; 

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

最後一個版本的優點是所有額外的檢測都不會編譯到您的發佈代碼中。

8

在調試時,當前目錄的路徑(程序執行的路徑)將位於。\ bin \ debug \ dir中。

當您部署該工具時,您不會再遇到此問題。

看來,你想用你的工具部署「銷售」目錄:所以也許你應該在你的解決方案中包括這個,並選擇它複製到你的可執行文件的輸出目錄(。\ bin \ debug) 。

2

嗯,我覺得這是更triccky的問題,因爲它似乎: 考慮到現在你有bin\Debug,對部署路徑消失,所以說GetCurrentDirectory(),我要說它能夠更好地使用Path.GetDirectoryName(Application.ExecutablePath),你會被檢索您的部署路徑。還有一天,您可能需要更改您的部署的文件結構,所以...

只需在您的bin \ Debug或bin \ Release中複製將來的部署文件/文件夾佈局,以便您的應用程序在DEBUG中工作並以相同的方式部署。總是如果可能的話。

希望這會有所幫助。