将许多变量写入文本文件

Writing many variables into textfile

我有这段代码:

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

它应该将所有这些变量保存到我说的文本文件中,但它只写入第一个变量 (numcont)。我需要做什么才能将我需要的所有变量写入该文本文件? 欢迎索取更多代码。

只需执行以下操作

您必须将所有变量连接到一个变量中,然后将其作为参数传递

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



writer.WriteLine(line);

或出于可读性原因

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

您需要格式化字符串:

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

如果你可以使用 C# 6 语法:

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

编辑

只使用年月日:

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

花样"yyyMMdd"可以随意编排。例如"dd.MM.yyyy"

使用 File.WriteAllText() 将简化您的代码,尝试 运行 this example 在线:

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

如果您查看方法 StreamWriter.WriteLine,您会发现 none 的重载与您输入该方法的参数模式相匹配。

到目前为止,您使用以下签名调用 this overload

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

文档中提到了参数 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.

这似乎不是你想要的。

显示对象的一种常用方法是重写 ToString 方法。

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

这将允许您确定格式,您可以简单地将整个对象作为参数转储到 WriteLine 方法中。 ToString 方法将被自动调用:

writer.WriteLine(_transaction);

您也可以使用 string.Concat(),因为它将所有值连接成一个字符串

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

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

重载方法如下

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