[英]Write to CSV and text file
In C# can someone teach me how to write the output of this program to a csv and a text file with unique ID each time? 在C#中,有人可以教我如何每次将此程序的输出写入csv和具有唯一ID的文本文件吗? Like instead of writing to the console I want everything to go to a csv file and a text file at the same time. 就像不写控制台一样,我希望所有内容都同时进入一个csv文件和一个文本文件。 And also for the txt file includes a unique ID as record keeping. 对于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();
}
}
} }
to a csv and a text file
, well,For writing to text file to a csv and a text file
,好,用于写入文本文件
use this for your req in some button click
: 在某些button click
中将其用于您的要求, 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();
}
And 和
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();
}
more details visit http://www.c-sharpcorner.com/resources/735/how-to-append-a-text-file-in-c-sharp.aspx 更多详细信息,请访问http://www.c-sharpcorner.com/resources/735/how-to-append-a-text-file-in-c-sharp.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.