2014-01-13 36 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