2017-07-28 58 views
0

删除一条线我有这样的代码:从我的文本文件

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

你能给什么样的电流结果的一个例子是,它应该是什么? – Laoujin

+0

更改您的过程。在循环外部写入事务项目以及循环内的事务细节。那么你不必回过头来弄脏文本文件。 –

+0

@Laoujin是的,当然,是这样的: 123456789; 0; 126; 28-07-2017; 7.98; 5; hdazs; 23; 0; 0; 3; 0 123456789; 0; 126; 28-07-2017; 7.98; 5;我想删除第二个“123456789; 0; 126; 28-07-2017; 7.98; 5;”;我想删除第二个“123456789; 0; 126; 28-07-2017; 7.98; 5;” –

回答

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}"); 
} 
+0

它的工作,谢谢 –