简体   繁体   English

C#Foreach循环创建一个新文件

[英]C# Foreach Loop create all time a new File

I would like to create a method that creates a new file for each supplier. 我想创建一个为每个供应商创建新文件的方法。

Unfortunately, my attempt doesn't work. 不幸的是,我的尝试不起作用。

It is only create one file. 它只创建一个文件。

My List contains 
SupplierNr     ProofNr
13245242       45519013979
50945724       45519104207
50851942       45519130964
50940487       45519139065
13496912       45519139000

It should create 5 files not only one. 它应该创建5个文件而不仅仅是一个。

My Method 我的方法

StreamWriter sw;
int filenumber = 1;
DateTime now = DateTime.Now;
string filename = $"C:\\Users\\pasca\\Desktop\\Test\\i.lle_alt.{now.ToString("yyyyMMdd")}{filenumber.ToString("D2")}";

foreach (string Supplier in SuppliersNr)
{
    int SPID = 0;

    sw = new StreamWriter(filename);

    for (int ID = 0; ID < CustomerProduct.Count; ID++)
    {
        sw.WriteLine($"{CustomerProduct[ID]};{DateBegin};{DateEnd};{Supplier};{Origin[ID]};{NonPreference[ID]};{ProofNr[SPID]};");
    }

    SPnr++;
    filenumber++;
    sw.Close();
}

Could you please explane me what i need to change, because i searched now a lot but doesn´t find any examples or something for my Question. 你能否解释一下我需要改变什么,因为我现在搜索了很多,但没有找到任何问题的例子或东西。

Although you increment filenumber you never update the variable filename inside the loop. 虽然你增加文件filenumber你永远不会更新循环内的变量filename

I would suggest to create a base filename part which is fixed (because it doesn't chage during the iteration: 我建议创建一个固定的基本文件名部分(因为它在迭代期间不会变形:

string filenameBase = $"C:\\Users\\pasca\\Desktop\\Test\\i.lle_alt.{now.ToString("yyyyMMdd")}"

and inside the loop change only the final part 并且在循环内部仅改变最终部分

foreach (string Supplier in SuppliersNr)
{
    sw = new StreamWriter(filenameBase + filenumber.ToString("D2"));
    filenumber++;       
}

you could actually get rid of the extra variable filenumber by using a normal for-loop. 你可以通过使用正常的for循环来摆脱额外的变量filenumber This way the increment will happen automatically: 这样增量将自动发生:

for (int i = 0; i < SuppliersNr.Count; i++) //<- assuming that SuppliersNr is a List<string>
{
    sw = new StreamWriter(filenameBase + (i + 1).ToString("D2"));       
}

You have to put filename creation inside the foreach loop. 您必须将filename创建放在foreach循环中。 Try like: 试试:

int filenumber = 1;
DateTime now = DateTime.Now;       
foreach (string Supplier in SuppliersNr)
{
  string filename = $"C:\\Users\\pasca\\Desktop\\Test\\i.lle_alt.{now.ToString("yyyyMMdd")}{filenumber.ToString("D2")}";
  filenumber ++;
....

filename is the same because you define it when filenumber was 1. filename是相同的,因为你在filenumber为1时定义它。
You need to update filename inside foreach loop: 你需要在foreach循环中更新filename

foreach (string Supplier in SuppliersNr)
{
    int SPID = 0;
    filename = $"C:\\Users\\pasca\\Desktop\\Test\\i.lle_alt.{now.ToString("yyyyMMdd")}{filenumber.ToString("D2")}";

    sw = new StreamWriter(filename);

    for (int ID = 0; ID < CustomerProduct.Count; ID++)
    {
        sw.WriteLine($"{CustomerProduct[ID]};{DateBegin};{DateEnd};{Supplier};{Origin[ID]};{NonPreference[ID]};{ProofNr[SPID]};");
    }

    SPnr++;
    filenumber++;
    sw.Close();
}

You should put the file name in this loop, like this: 您应该将文件名放在此循环中,如下所示:

            foreach (string Supplier in SuppliersNr)
            {
                string filename = $"C:\\Users\\pasca\\Desktop\\Test\\i.lle_alt.{now.ToString("yyyyMMdd")}{filenumber.ToString("D2")}";
                sw = new StreamWriter(filename);

                //your logic
                //fileNumber++;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM