Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在编写.txt文档时,如何更改文件夹位置的路径?_C#_Path_Substring_Filestream_Lastindexof - Fatal编程技术网

C# 在编写.txt文档时,如何更改文件夹位置的路径?

C# 在编写.txt文档时,如何更改文件夹位置的路径?,c#,path,substring,filestream,lastindexof,C#,Path,Substring,Filestream,Lastindexof,我正在使用filestream编写文本文档。除了将文档定向到错误的文件夹外,其他一切似乎都正常工作 这是代码认为应该创建文档的地方: C:\Users\user2\Desktop\work\SuperBy\nop\NopCommerceStore\Administration\FedEx 管理文件夹中不存在联邦快递的文件夹。但是,如果我创建一个FedEx文件夹并将其放在Administration文件夹中,则.txt文档会相应出现 这是应该写入文档的文件夹的实际路径: C:\Users\use

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

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

C:\Users\user2\Desktop\work\SuperBy\nop\NopCommerceStore\Administration\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);
}
试试看
{
Order o=IoC.Resolve().GetOrderById(orderID);
客户ThisCustomer=o.客户;
//指定文件、说明和权限
字符串路径=System.Web.HttpContext.Current.Request.PhysicalPath;
int index=path.LastIndexOf(“\\”);
字符串realPath=path.Substring(0,索引+1);
FileStream file=newfilestream(realPath+“/FedEx/”+orderID.ToString()+“.txt”、FileMode.OpenOrCreate、FileAccess.Write);
}
试试看
{
Order o=IoC.Resolve().GetOrderById(orderID);
客户ThisCustomer=o.客户;
//指定文件、说明和权限
字符串路径=System.Web.HttpContext.Current.Request.PhysicalPath;
int index=path.LastIndexOf(“\\”);
字符串realPath=path.Substring(0,索引+1);
FileStream file=新FileStream(realPath+“/../FedEx/”+
orderID.ToString()+“.txt”,FileMode.OpenOrCreate,FileAccess.Write);
}
试试看
{
Order o=IoC.Resolve().GetOrderById(orderID);
客户ThisCustomer=o.客户;
//指定文件、说明和权限
字符串路径=System.Web.HttpContext.Current.Request.PhysicalPath;
int index=path.LastIndexOf(“\\”);
字符串realPath=path.Substring(0,索引+1);
FileStream file=新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;

    // 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);
}
试试看
{
Order o=IoC.Resolve().GetOrderById(orderID);
客户ThisCustomer=o.客户;
//指定文件、说明和权限
字符串路径=System.Web.HttpContext.Current.Request.PhysicalPath;
//这应该给我一个索引,其中“\administration”部分是
//路径开始,所以我们可以切断它
int index=path.IndexOf(\\管理“,
对照组(例);
//在构建路径时,我们将从0子串到
//“\administration”字符串的一部分,添加订单id“FedEx”,
//最后是“.txt”
字符串realPath=Path.Combine(Path.Substring(0,索引),“FedEx”,
orderID.ToString(),“.txt”);
//现在打开文件
FileStream file=新的FileStream(realPath,
FileMode.OpenOrCreate,
FileAccess.Write);
}

另外,
Path.Combine
在这里是非常关键的。在构建路径时,将
\
放在正确的位置很快就会出现问题--它可以无缝地处理这些问题。

基于您的陈述,我相信您希望创建如下路径:

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);
}
试试看
{
Order o=IoC.Resolve().GetOrderById(orderID);
客户ThisCustomer=o.客户;
//指定文件、说明和权限
字符串路径=System.Web.HttpContext.Current.Request.PhysicalPath;
//这应该给我一个索引,其中“\administration”部分是
//路径开始,所以我们可以切断它
int index=path.IndexOf(\\管理“,
对照组(例);
//在构建路径时,我们将从0子串到
//“\administration”字符串的一部分,添加订单id“FedEx”,
//最后是“.txt”
字符串realPath=Path.Combine(Path.Substring(0,索引),“FedEx”,
orderID.ToString(),“.txt”);
//现在打开文件
FileStream file=新的FileStream(realPath,
FileMode.OpenOrCreate,
FileAccess.Write);
}

另外,
Path.Combine
在这里是非常关键的。在构建路径时,如果将
\
放在正确的位置,很快就会出现问题--它会无缝地处理这些问题。

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

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


因此,在将联邦快递附加到
realPath
System.Web.HttpContext.Current.Request.PhysicalPath
之前,需要删除
\Administration

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


因此,在将联邦快递附加到realPath之前,您需要删除管理,您可以将物品放在任何您想要的地方。如果您想控制此功能,可以使用:

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);
}
试试看
{
Order o=IoC.Resolve().GetOrderById(orderID);
客户ThisCustomer=o.客户;
FileStream file=new FileStream(“C:/Users/user2/Desktop/work/SuperBy/nop/nopcmercestore/FedEx/”+orderID.ToString()+“.txt”、FileMode.OpenOrCreate、FileAccess.Write);
}

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

你可以给物品任何你想要的位置。如果您想控制此功能,可以使用:

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);
}
试试看
{
Order o=IoC.Resolve().GetOrderById(orderID);
客户ThisCustomer=o.客户;
FileStream file=new FileStream(“C:/Users/user2/Desktop/work/SuperBy/nop/nopcmercestore/FedEx/”+orderID.ToString()+“.txt”、FileMode.OpenOrCreate、FileAccess.Write);
}
现在更明显的是,如何更改代码以使用不同的