我有这段代码:
using (StreamWriter writer = new StreamWriter("C:\\Users\\HP8200\\Desktop\\teste.txt"))
{
string numcont = _transaction.PartyFederalTaxID;
double numenc = _transaction.BillToPartyID;
double numfatura = _transaction.TransDocNumber;
DateTime data = _transaction.CreateDate;
double valor = _transaction.TotalAmount;
short zona = transaction.Zone.ZoneID;
//preçoantesdisc mal
double precoantesdisc = _transaction.TotalPaymentDiscountAmount;
double preconet = transaction.Details.TotalAdvancementNetAmount;
double quantidade = transaction.Details.Count;
double bonus = _transaction.TotalPaymentDiscountAmount;
string valorStr = valor.ToString(CultureInfo.InvariantCulture);
foreach (ItemTransactionDetail detail in transaction.Details)
{
var item = MyApp.DSOCache.ItemProvider.GetItem(detail.ItemID, MyApp.SystemSettings.BaseCurrency);
double taxRate = MyApp.DSOCache.TaxesProvider.GetTaxRateFromTaxableGroupID(detail.TaxableGroupID, "PRT", "CON");
string barcode = item.BarCode;
item = null;
writer.WriteLine($"{numcont};{numenc};{numfatura};{data.ToString("dd/MM/yyyy")};{valorStr};{zona};{Environment.NewLine}{barcode};{taxRate};{precoantesdisc};{preconet};{quantidade};{bonus}");
}
}
MessageBox.Show("gravou");
它将一些变量写入文本文件。当我向其中添加2个或更多项时,文本将重复第一行。我需要删除第三行的第一行。 (通过第一行和第二行,我的意思是直到{Environment.NewLine}
)。
有可能吗?
答案 0 :(得分:3)
您可以完全控制重复的行,因此您应该修复 而不是返回并尝试将其从结果中删除:
writer.WriteLine($"{numcont};{numenc};{numfatura};{data:dd/MM/yyyy)};{valorStr};{zona};");
foreach (ItemTransactionDetail detail in transaction.Details)
{
var item = MyApp.DSOCache.ItemProvider.GetItem(detail.ItemID, MyApp.SystemSettings.BaseCurrency);
double taxRate = MyApp.DSOCache.TaxesProvider.GetTaxRateFromTaxableGroupID(detail.TaxableGroupID, "PRT", "CON");
string barcode = item.BarCode;
//item = null; //not necessary - GC will take care of this
writer.WriteLine($"{barcode};{taxRate};{precoantesdisc};{preconet};{quantidade};{bonus}");
}