C# Foreach Loop create all time a new File
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.
4 Answers
4
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. This way the increment will happen automatically:
filenumber
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. Try like:
filename
foreach
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.
You need to update filename inside foreach loop:
filename
filenumber
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++;
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.