TheStupidOne
TheStupidOne

Reputation: 37

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.

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.

Upvotes: 3

Views: 1216

Answers (4)

Tomas Chabada
Tomas Chabada

Reputation: 3019

filename is the same because you define it when filenumber was 1.
You need to update filename inside foreach loop:

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

Upvotes: 1

Mong Zhu
Mong Zhu

Reputation: 23732

Although you increment filenumber you never update the variable filename inside the loop.

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

Upvotes: 4

Antoine V
Antoine V

Reputation: 7204

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

Upvotes: 0

apomene
apomene

Reputation: 14389

You have to put filename creation inside the foreach loop. 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 ++;
....

Upvotes: 1

Related Questions