2014-01-13 38 views
0

我正在使用filestream來編寫文本文檔。除了將文檔導向到錯誤的文件夾之外,一切看起來都很好。如何在編寫.txt文檔時更改文件夾位置的路徑?

這是代碼認爲它應該創建文檔:

C:\Users\user2\Desktop\work\SuperBy\nop\NopCommerceStore\Administration\FedEx 

FedEx文件夾不到風度存在於管理文件夾。但是,如果我創建一個FedEx文件夾並將其放入Administration文件夾中,則會相應地顯示.txt文檔。

這是實際的路徑就在該文件應該被寫入該文件夾:

C:\Users\user2\Desktop\work\SuperBy\nop\NopCommerceStore\FedEx 

我根本不明白的代碼是如何工作的,以及如何去改變它,真的需要一些幫助與此有關。

try 
{ 
    Order o = IoC.Resolve<IOrderService>().GetOrderById(orderID); 
    Customer ThisCustomer = o.Customer; 

    // Specify file, instructions, and privelegdes 
    string path = System.Web.HttpContext.Current.Request.PhysicalPath; 
    int index = path.LastIndexOf("\\"); 
    string realPath = path.Substring(0, index + 1); 
    FileStream file = new FileStream(realPath + "/FedEx/" + orderID.ToString() + ".txt", FileMode.OpenOrCreate, FileAccess.Write); 
} 
+0

realPath +「/../FedEx/」可能有幫助 –

+0

謝謝Thomas! – koffe14

回答

2
try 
{ 
    Order o = IoC.Resolve<IOrderService>().GetOrderById(orderID); 
    Customer ThisCustomer = o.Customer; 

    // Specify file, instructions, and privelegdes 
    string path = System.Web.HttpContext.Current.Request.PhysicalPath; 
    int index = path.LastIndexOf("\\"); 
    string realPath = path.Substring(0, index + 1); 
    FileStream file = new FileStream(realPath + "/../FedEx/" + 
     orderID.ToString() + ".txt", FileMode.OpenOrCreate, FileAccess.Write); 
} 
+0

謝謝!它工作出色 – koffe14

2

立足它關閉你的陳述,我相信你想創建這樣的路徑:

try 
{ 
    Order o = IoC.Resolve<IOrderService>().GetOrderById(orderID); 
    Customer ThisCustomer = o.Customer; 

    // Specify file, instructions, and privelegdes 
    string path = System.Web.HttpContext.Current.Request.PhysicalPath; 

    // this should give me the index at which the "\administration" part of the 
    // path starts so we can cut it off 
    int index = path.IndexOf("\\administration", 
     StringComparison.OrdinalIgnoreCase); 

    // when building the path we substring from 0 to the index of the 
    // "\administration" part of the string, add "FedEx", the order id, 
    // and then finally ".txt" 
    string realPath = Path.Combine(path.Substring(0, index), "FedEx", 
     orderID.ToString(), ".txt"); 

    // now open the file 
    FileStream file = new FileStream(realPath, 
     FileMode.OpenOrCreate, 
     FileAccess.Write); 
} 

此外,Path.Combine真的是這裏的關鍵。在建立路徑時,它可能會很快成爲問題,在正確的位置獲得\ - 它可以無縫地處理這個問題。

+0

謝謝!我不熟悉組合,但我在web.config中的「appsettings」中創建了一個「addkey」,並使用本地鏈接作爲值,你知道c:/ user/.. etc但是使用lokal鏈接似乎並不像成爲最好的主意,我寧願嘗試一個更抽象的解決方案。我目前在localhost上運行NopCommerce,但稍後會在服務器上上傳。感謝提示=) – koffe14

+0

它沒有工作。我看到你wroye \\管理,但該文件被保存在NopCommerceStore。 Meaby它可以改變文件夾的位置? – koffe14

+0

@KristofferAndersson,'\\ Administration'的想法是找到它開始的索引,這樣我就可以將其切斷。有可能字符串比較需要非序數,因爲我注意到你用小寫'a'寫了'\\ administration'。 –

0

System.Web.HttpContext.Current.Request.PhysicalPath正在恢復,

C:\用戶\用戶2 \桌面\工作\ SuperBy \ NOP \ NopCommerceStore \管理,

因此,你需要追加FedExrealPath前下降\Administration

+0

我明白了,但我該如何改變物理路徑,因爲它目前是指向管理文件夾?我的意思是我該如何放棄它? – koffe14

0

你可以給物品任何你想要的地方。如果你想控制這個,你可以使用:

try 
{ 
    Order o = IoC.Resolve<IOrderService>().GetOrderById(orderID); 
    Customer ThisCustomer = o.Customer; 
    FileStream file = new FileStream("C:/Users/user2/Desktop/work/SuperBy/nop/NopCommerceStore/FedEx/" + orderID.ToString() + ".txt", FileMode.OpenOrCreate, FileAccess.Write); 
} 

現在它更明顯如何改變你的代碼使用不同的路徑。對於任何需要閱讀它的人來說,保持你的代碼可讀性是非常重要的。在這種情況下,就是你自己。

+1

我想這是有道理的。這更可讀。感謝您的輸入! – koffe14