写入CSV和文本文件

时间:2012-12-06 06:10:18

标签: c# text csv

在C#中,有人可以教我如何将此程序的输出写入csv和每次具有唯一ID的文本文件吗?喜欢而不是写入控制台,我希望所有内容同时转到csv文件和文本文件。此外,txt文件还包含一个唯一的ID作为记录保存。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{

public class Product
{
    public string description;
    public double price;
    public int count;

    public Product(string description, double price, int count)
    {
        this.description = description;
        this.price = price * count;
        this.count = count;
    }

    public string GetDetailLine()
    {

        return String.Format(count + " {0}: {1:c}", description, price);
    }
}

class Program
{
    static void Main(string[] args)
    {
        int AdultTicket;
        int KidTicket;
        int PopcornOrder;
        int DrinkOrder;
        double tax = .05;

        List<Product> products = new List<Product>();

        Console.Write("How many adults? ");
        int.TryParse(Console.ReadLine(), out AdultTicket);
        Product adultTicket = new Product("Adult Ticket(s)", 7.75, AdultTicket);
        products.Add(adultTicket);

        Console.Write("How many kids? ");
        int.TryParse(Console.ReadLine(), out KidTicket);
        Product kidTicket = new Product("Kid Ticket(s)", 5.75, KidTicket);
        products.Add(kidTicket);

        Console.Write("Do you want popcorn? ");
        string input = Console.ReadLine();
        if (input.ToUpper() == "YES")
        {
            Console.Write ("How many? ");
            int.TryParse(Console.ReadLine (), out PopcornOrder);
            Product popcorn = new Product("Small popcorn", 3.50, PopcornOrder );
            products.Add(popcorn);
        }

        Console.Write("Do you want a drink? ");
        string input2 = Console.ReadLine();
        if (input2.ToUpper() == "YES")
        {
            Console.Write("How many? ");
            int.TryParse(Console.ReadLine(), out DrinkOrder);
            Product drink = new Product("Large Soda", 5.50, DrinkOrder);
            products.Add(drink);
            Console.Clear();
        }



        int count = 0;
        for (int i = 0; i < products.Count; i++)
        {
            count += products[i].count;
        }

        double total = 0;
        for (int i = 0; i < products.Count; i++)
        {                
            Console.WriteLine("\t\t" + products[i].GetDetailLine());
            total += products[i].price;
        }

        Console.WriteLine("\nYour sub total is:  {0:c}", total);
        Console.WriteLine("Tax: {0:c}", total * tax);
        Console.WriteLine("Your total is: {0:c}", total * tax + total);
        Console.ReadLine();
    }
}

}

1 个答案:

答案 0 :(得分:0)

to a csv and a text file,好吧,用于写入文本文件

在某些button click

中使用此功能
       private static string logFileName = @"C:\Junk\MyLogfile.txt";
       using (StreamWriter writer = File.AppendText(logFileName))
            {
                Log("Your sub total is:"+total.toString(), writer);
                Log("Tax:"+ (total * tax).toString(), writer);
                Log("Your total is:"+(total * tax + total).toString(), writer);
                writer.Close();
            }

        public static void Log(String logMessage, TextWriter writer)
        {
            writer.Write("\r\nLog Entry : ");
            writer.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                DateTime.Now.ToLongDateString());
            writer.WriteLine("  :");
            writer.WriteLine("  :{0}", logMessage);
            writer.WriteLine("-------------------------------");
            // Update the underlying file.
            writer.Flush();
        }

更多详情,请访问http://www.c-sharpcorner.com/resources/735/how-to-append-a-text-file-in-c-sharp.aspx

相关问题