2017-07-28 54 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

它的工作,謝謝 –