【问题标题】:How do i change path to folder location when writing a .txt document?编写 .txt 文档时如何更改文件夹位置的路径?
【发布时间】:2014-01-13 15:46:24
【问题描述】:

我正在使用filestream 编写文本文档。除了文档被定向到错误的文件夹之外,一切似乎都正常。

这是代码认为它应该创建文档的地方:

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);
}

【问题讨论】:

  • realPath + "/../FedEx/" 可能有帮助

标签: c# path substring filestream lastindexof


【解决方案1】:
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);
}

【讨论】:

    【解决方案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 在这里真的很关键。在构建路径时,将\ 放在正确的位置会很快成为问题——它可以无缝处理。

    【讨论】:

    • 谢谢!我不熟悉组合,但我在 web.config 的“appsettings”中创建了一个“addkey”,并使用本地链接作为值,你知道 c:/user/..etc 但是使用 lokal 链接似乎没有成为最好的主意,我宁愿尝试一个更抽象的解决方案。我目前在本地主机上运行 NopCommerce,但稍后会将其上传到服务器上。感谢您的提示 =)
    • 它没有用。我看到你 wroye \\administration 但该文件应该保存在 NopCommerceStore 中。 Meaby 它可以更改文件夹位置吗?
    • @KristofferAndersson,\\Administration 的想法是找到它开始的索引,以便我可以将其切断。字符串比较可能需要是非序数的,因为我注意到您使用小写 a 编写了 \\administration。
    【解决方案3】:

    System.Web.HttpContext.Current.Request.PhysicalPath 正在返回,

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

    因此,您需要先删除\Administration,然后再将FedEx 附加到realPath。

    【讨论】:

    • 我明白了,但是我如何更改当前指向管理文件夹的物理路径?我的意思是我该如何放下它?
    【解决方案4】:

    你可以把物品放在你想要的任何地方。如果您想对此进行控制,可以使用:

    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);
    }
    

    现在更明显的是如何更改代码以使用不同的路径。让任何应该阅读它的人都能阅读您的代码,这一点非常重要。在这种情况下就是你自己。

    【讨论】:

    • 我想这是有道理的。这更具可读性。感谢您的投入!
    猜你喜欢
    • 2012-06-07
    • 2017-05-28
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多