8

I've this piece of code:

using (StreamWriter writer = new StreamWriter("C:\\Users\\HP8200\\Desktop\\teste.txt"))
{            
    string numcont = _transaction.PartyFederalTaxID;

    double numenc = _transaction.BillToPartyID;
    double numfatura = _transaction.BillToPartyID;
    string zona = _transaction.BillToPartyCountryID;
    DateTime data = _transaction.CreateDate;
    string ean = _transaction.ATDocCodeId;
    double iva = _transaction.TotalTaxAmount;
    double precoantesdisc = _transaction.TotalLineItemDiscountAmount;
    double preconet = _transaction.TotalNetAmount;

    writer.WriteLine(numcont,";", numenc,";", numfatura,";", data,";", zona, Environment.NewLine , 
        ean,";", iva,";", precoantesdisc,";", preconet);
}

MessageBox.Show("gravou");

And it should save all those variables into the textfile I say but it only writes the first variable(numcont). What I need to do so it writes all the variables I need on that textfile? Feel free to ask for more code.

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
joao costa
  • 141
  • 10

5 Answers5

7

You need to format the string:

writer.WriteLine(string.Format("{0};{1};{2};{3};{4}{5}{6};{7};{8};{9}",numcont, numenc, numfatura, data.ToString(), zona, Environment.NewLine, ean, iva, precoantesdisc, preconet));

If you can use C# 6 syntax:

writer.WriteLine($"{numcont};{numenc};{numfatura};{data.ToString()};{zona}{Environment.NewLine}{ean};{iva};{precoantesdisc};{preconet}");

EDIT

Use only day, month and year:

writer.WriteLine($"{numcont};{numenc};{numfatura};{data.ToString("yyyyMMdd")};{zona}{Environment.NewLine}{ean};{iva};{precoantesdisc};{preconet}");

The patter "yyyMMdd" can be arranged as you want. For example "dd.MM.yyyy"

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
5

just do the following

you have to concatenate all variables in a variable and then pass it as an argument

var line = numcont +";"+ numenc+";"+ numfatura+";"+ data+";"+ zona +Environment.NewLine + ean+";"+ iva+";"+ precoantesdisc+";"+ preconet; 



writer.WriteLine(line);

or for readibilty reasons

var  line =string.Format("{0};{1};{2};{3};{4}\n{5};{6};{7};{8}",numcont, numenc, numfatura, data.ToString(), zona, ean, iva, precoantesdisc, preconet);
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
1

Use File.WriteAllText() will simplify your code, try run this example online:

public static void Main()
{
    string numcont = "abc";

    double numenc = 1.1;
    double numfatura = 12.2;
    string zona = "def";
    DateTime data = new DateTime();
    string ean = "hij";
    double iva = 23.4;
    double precoantesdisc = 34.5;
    double preconet = 45.6;
    var info =  numcont + ";" + numenc + ";" + numfatura + ";" + data + ";"
                + zona + Environment.NewLine + ean +";" + iva + ";" + 
                precoantesdisc +";" + preconet;
    File.WriteAllText("abc.txt",info);

    Console.Write(info);
}
David
  • 15,894
  • 22
  • 55
  • 66
1

If you look at the method StreamWriter.WriteLine you will see that none of the overloads matches the pattern of parameters that you feed into the method.

Up to now you are calling this overload with the signature of:

public virtual void WriteLine(<br> string format,<br> params object[] arg<br> )

The documentation says about the parameter arg

An object array that contains zero or more objects to format and write....
The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. The formatting process replaces each format item with the string representation of the value of the corresponding object.

This seems not what you want.

One often used method for the display of an object is to override the ToString method.

public override string ToString()
{
    return $"{PartyFederalTaxID};{BillToPartyID};{CreateDate};{BillToPartyCountryID}" + Environment.NewLine + 
    $"{ATDocCodeId};{TotalTaxAmount}{TotalLineItemDiscountAmount}{TotalNetAmount}"
}

this will allow you to determine the format and you could simply dump the entire object as parameter into the WriteLine method. The ToString method will be called automatically:

writer.WriteLine(_transaction);
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
0

You can also use string.Concat() as it will concatenate all the values to a string

writer.WriteLine(string.Concat(numcont,";", numenc,";", numfatura,";", data,";", zona, Environment.NewLine , 
    ean,";", iva,";", precoantesdisc,";", preconet));

or

StreamWriter.WriteLine(string format, params object[] arg) 

overload method as below

writer.WriteLine("{0};{1};{2};{3};{4};{5};{6}:{7};{8};{9}", numcont, numenc, numfatura, data, zona, Environment.NewLine,
                ean, iva, precoantesdisc, preconet);
Saravanan Sachi
  • 2,572
  • 5
  • 33
  • 42