سؤال

I'm using filestream to write a text document. Everything seems to work fine except that the document is directed to the wrong folder.

This is where the code thinks it should create the document:

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

The FedEx folder dosn't exist in the Administration folder. But if I create a FedEx folder and place it in the Administration folder, the .txt documents appear accordingly.

This is the actual path do the folder in which the documents should be written:

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

I simply don't understand how the code works and how to change it, really need some help with this.

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);
}
هل كانت مفيدة؟

المحلول

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

نصائح أخرى

Basing it off your statements, I believe you want to create the path like this:

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

Also, Path.Combine is really key here. When building paths it can very quickly become problematic getting \ in the right places--it handles that seamlessly.

System.Web.HttpContext.Current.Request.PhysicalPath is returning,

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

Thus, you need drop \Administration before appending FedEx to realPath.

You can give the item any place you want. If you want to have control over this you could use:

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

Now its more obvious how you can change your code to use a different path. Its very important to keep your code readable to anyone who should ever read it. In this case that is yourself.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top