更改文本文件的名称

时间:2017-07-31 08:25:40

标签: c#

我有这段代码:

 using (StreamWriter writer = new StreamWriter("C:\\Users\\HP8200\\Desktop\\fatura.txt"))
    {            
        string numcont = _transaction.PartyFederalTaxID;
        double numenc = _transaction.BillToPartyID;
        double numfatura = _transaction.TransDocNumber;
        DateTime data = _transaction.CreateDate;
        double valor = _transaction.TotalAmount;
        short zona = transaction.BillToPartyAddressID;
        double precoantesdisc = bsoItemTrans.BSOItemTransactionDetail.TransactionDetail.PackUnitPrice;
        double preconet = transaction.TotalTransactionAmount;
        double quantidade = transaction.Details.Count;
        double bonus = _transaction.TotalPaymentDiscountAmount;
        string valorStr = valor.ToString(CultureInfo.InvariantCulture);
        string preconetStr = preconet.ToString(CultureInfo.InvariantCulture);
        string precoantesdiscStr = precoantesdisc.ToString(CultureInfo.InvariantCulture);           

            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;
                writer.WriteLine($"{barcode};{taxRate};{precoantesdiscStr};{preconetStr};{quantidade};{bonus}");
            }

        }
        MessageBox.Show("gravou");
    }

我需要将文本文件的名称更改为帐单编号(在这种情况下是变量“numfactura”)。 这可能吗?

我需要为每个账单创建一个新文件。

这可能吗?

2 个答案:

答案 0 :(得分:6)

查看System.IO.File.Move()方法,将Move()文件更改为新名称。

System.IO.File.Move("OldFileName", "NewFileName");

答案 1 :(得分:0)

double numfatura = _transaction.TransDocNumber;
string fileName = numfatura + ".txt";
using (StreamWriter writer = new StreamWriter(fileName))
{            

我不知道你如何列举你的账单,但是把你的编写代码放在foreach循环中。