happysmile
happysmile

Reputation: 7777

how to format data in a text file

ihave an string builder where it conatins email id( it conatins thousands of email id)

StringBuilder sb = new StringBuilder();
foreach (DataRow dr2 in dtResult.Rows)
{
    strtxt = dr2[strMailID].ToString()+";";
    sb.Append(strtxt);    
}

 string filepathEmail = Server.MapPath("Email");
 using (StreamWriter outfile = new StreamWriter(filepathEmail + "\\" + "Email.txt"))
 {
     outfile.Write(sb.ToString());
 }

now data is getting stored in text file like this:

[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected]; [email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];

But i need to store them like where every row should only only 10 email id, so that i looks good**

any idea how to format the data like this in .txt file? any help would be great

Upvotes: 2

Views: 386

Answers (5)

Guffa
Guffa

Reputation: 700302

Use a counter and add a line break each tenth item:

StringBuilder sb = new StringBuilder();
int cnt = 0;
foreach (DataRow dr2 in dtResult.Rows) {
  sb.Append(dr2[strMailID]).Append(';');
  if (++cnt == 10) {
    cnt = 0;
    sb.AppendLine();
  }
}
string filepathEmail = Path.Combine(Server.MapPath("Email"), "Email.txt");
File.WriteAllText(filepathEmail, sb.ToString());

Notes:

  • Concatentate strings using the StringBuilder instead of first concatenating and then appending.
  • Use Path.Combine to combine the path and file name, this works on any platform.
  • You can use the File.WriteAllText method to save the string in a single call instead of writing to a StreamWriter.

Upvotes: 1

Jeff Mercado
Jeff Mercado

Reputation: 134841

Here's an alternate method using LINQ if you don't mind any overheads.

string filepathEmail = Server.MapPath("Email");
using (StreamWriter outfile = new StreamWriter(filepathEmail + "\\" + "Email.txt"))
{
    var rows = dtResult.Rows.Cast<DataRow>(); //make the rows enumerable
    var lines = from ivp in rows.Select((dr2, i) => new {i, dr2})
                group ivp.dr2[strMailID] by ivp.i / 10 into line //group every 10 emails
                select String.Join(";", line); //put them into a string

    foreach (string line in lines)
        outfile.WriteLine(line);
}

Upvotes: 0

Andrea Parodi
Andrea Parodi

Reputation: 5614

Use a counter to keep track of number of mail already written, like this:

        int i = 0;
        foreach (string mail in mails) {
            var strtxt = mail + ";";
            sb.Append(strtxt);
            i++;
            if (i % 10==0)
                sb.AppendLine();
        }

Every 10 mails written, i modulo 10 equals 0, so you put an end line in the string builder. Hope this can help.

Upvotes: 0

Arseny
Arseny

Reputation: 7351

as it said you may add a "line break" I suggest to add '\t' tab after each address so your file will be CSV format and you can import it in Excel for instance.

Upvotes: 0

Oded
Oded

Reputation: 498982

Just add a counter in your loop and append a line break every 10 lines.

int counter = 0;
StringBuilder sb = new StringBuilder();
foreach (DataRow dr2 in dtResult.Rows)
{
   counter++;
   strtxt = dr2[strMailID].ToString()+";";
   sb.Append(strtxt);
   if (counter % 10 == 0)
   {
     sb.Append(Environment.NewLine);
   }
}

Upvotes: 2

Related Questions