簡體   English   中英

Path.Combine()無法正常工作

[英]Path.Combine() is not working as expected

 var storePath = ConfigurationManager.AppSettings[configKey]; 
 var dbpath=dbpath.replace("/","\\")
 var fullFilePath = Path.Combine(storePath, dbpath);

存儲在配置密鑰-> d:\\Storage\\ResourceStorage

數據庫中的值: dbpath: LearnerAnswers\\test.pkg

預期輸出: d:\\Storage\\ResourceStorage\\LearnerAnswers\\test.pkg

實際輸出: D:\\LearnerAnswers\\test.pkg

更新了問題以反映確切的情況

調試器的存儲路徑值: d:\\Storage\\ResourceStorage

我花了很多時間。.但是找不到什么地方出了問題嗎?

我已經檢查了您在問題中給出的示例路徑,並且得到了預期的輸出。

var storePath = @"d:\Storage\ResourceStorage"; 
var dbpath = @"LearnerAnswers\test.pkg"; 
var fullFilePath = Path.Combine(storePath, dbpath);

肯定還有其他錯誤。 請在單步模式下使用調試器,並驗證每個單個值。

以下原始答案是由於最初在問題中提供的信息無效。

您需要在此處引用反斜杠或使用 @

 
 
 
  
  var storePath = "d:\\Storage\\ResourceStorage";
 
  

因此,請使用以下之一:

 
 
 
  
  var storePath = @"d:\\Storage\\ResourceStorage"; var storePath = "d:\\\\Storage\\\\ResourceStorage";
 
  

DBPath是否以"\\\\"開頭?

如果第二個變量以"\\\\"@"\\"開頭,則Path.Combine假定您需要根目錄

Path.Combine("C:\\\\test", "\\\\NewFolder")返回"c:\\\\NewFolder"

Path.Combine("C:\\\\test", "NewFolder")返回"c:\\\\test\\\\NewFolder"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM