2012-12-06 71 views
0

在C#中有人可以教我如何將這個程序的輸出寫入一個csv和每次都有唯一ID的文本文件嗎?喜歡而不是寫入控制檯,我希望一切都能同時進入csv文件和文本文件。而且對於txt文件還包括一個唯一的ID作爲記錄保存。寫入CSV和文本文件

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(); 
    } 
} 

}

+0

您是指CSV文件(逗號分隔值)還是CVS,源代碼管理系統? –

+0

CSV,逗號分隔值。抱歉,是我的錯。 –

+0

不用擔心。它在答案中有所不同。看看String類的Join成員。它允許您使用選定的分隔符來加入字符串集合。 http://msdn.microsoft.com/en-us/library/tk0xe5h0.aspx –

回答

0

to a csv and a text file,那麼,對於一些button click書面文本文件

使用此爲您的REQ:

 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

+0

編譯器給了我一個錯誤,說「期望的類,委託,枚舉,接口或結構」。當我嘗試這兩個選項 第一次使用這個,所以我不知道它是什麼意思。你能詳細說明一下嗎? –

+0

此代碼是針對您的要求手寫的,進行必要的更改,也可以查看此鏈接瞭解更多詳情http://www.c-sharpcorner.com/resources/735/how-to-append-a-text-file-in -C-sharp.aspx – sajanyamaha