2013-04-03 87 views
-1
strFilePath = System.Web.HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) 
strFilePath = str + "ApInterface_" + Format(Now.Date, "dd-MMM-yyyy").Replace("-", "") + "_" + Format(Now, "HH:mm:ss").Replace(":", "") + ".dat" 

我上面的代碼片斷它在指定的保存文件中的.dat擴展名我的問題是關於路徑。當我指定的路徑如「D:\ myfolder」,數據將被導出,文件將被打開,但它不會得到保存。如果我指定文件夾爲「D:\ myfolder \」它可以完美保存爲什麼我需要"\"並最終? 可以任何一個解釋我。請幫助。它非常緊急!急切等待答案如何知道System.Web.HttpContext.Current.Server.MapPath路徑(HttpContext.Current.Request.ApplicationPath)

回答

0

使用Path.Combine而不是字符串連接。它會在需要時添加斜槓。

0

應該不是你的代碼是這樣的:

strFilePath = System.Web.HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) 
strFilePath = strFilePath + "ApInterface_" + Format(Now.Date, "dd-MMM-yyyy").Replace("-", "") + "_" + Format(Now, "HH:mm:ss").Replace(":", "") + ".dat" 

因爲它的立場,也沒有什麼「STR」持有的解釋。

假設以上是正確的,你需要的 「\」 的原因是因爲沒有它有兩種截然不同的路徑:

  • d:\ myfolderApInterface_01012001_010101.dat
  • d:\ MyFolder文件\ ApInterface_01012001_010101 .DAT

第一引用一個在「d」驅動器的根目錄中稱爲「myfolderApInterface_01012001_010101.dat」的文件,而第二參考文件名爲「ApInterface_01012001_010101.dat」中的「MyFolder文件」目錄「D」驅動器。

正如其他人提到的那樣,您可以使用Path.Combine來確定是否已經存在「\」,並且只在需要時添加它。

P.S.您可能還想考慮使用string.Format來構建文件名以提高可讀性

+0

str是一個字符串,它從文本框中讀取值(其中,我可以指定可以導出我的.dat文件的文件夾) – user2017976

+0

if多數民衆贊成的情況下,你的代碼的第一行是多餘的,但上述解釋。如果沒有斜槓,則會將目錄和文件名合併爲一個文件名 – kaykayman

相關問題