我有这段代码:
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).我需要做什么,它在文本文件中写下我需要的所有变量?
随意请求更多代码.
解决方法
您需要格式化字符串:
writer.WriteLine(string.Format("{0};{1};{2};{3};{4}{5}{6};{7};{8};{9}",numcont,data.ToString(),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”