[英]How to combine paths by preserving the original path's directory separator in C#?
[英]How to open directory few levels down of original path C#?
我需要從執行應用程序的位置向下三級打開一個文件夾(原始示例存在一些缺陷):
// find the path where the executable resides
string dbPath = Application.StartupPath;
// constructing the connection string - double slashes
string connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
+ dbPath + "\\..\\..\\..\\Magazines.accdb; User Id=admin; Password=";
但這將打開:
C:\Documents and Settings\Server\Desktop\Lab 10\Lab 10\Lab 10\bin\Debug\..\..\..\Magazines.accdb
程序開始的原始目錄:
C:\Documents and Settings\Server\Desktop\Lab 10\Lab 10\Lab 10\bin\Debug\
我需要它是:
C:\Documents and Settings\Server\Desktop\Lab 10\Lab 10\Magazines.accdb
這樣做的正確之處是什么?
使用DirectoryInfo評估“ ..”
var path = new DirectoryInfo (Path.Combine( "c:/bla", "../newBla")).FullName()
還可以使用Path.Combine
來簡化和更可靠的組合。
更改Magazines.accdb的屬性,以便在構建項目時將其復制到bin \\ Debug文件夾。
您可以通過在解決方案資源管理器中右鍵單擊文件,然后將“復制到輸出目錄”更改為“始終復制”來執行此操作。
幾個選項只是將數據庫與可執行文件放在同一文件夾中(因此,在調試文件夾中),這實際上是有道理的,因為當您將可執行文件打包到某個人時,通常會得到結果文件夾結構。
另一個選擇是,如果數據庫文件已添加到項目中,則可以將“復制到輸出目錄”指定為“如果更新則復制”,如果更新則將復制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.