2013-08-28 206 views
0

我試圖將我的數據輸出到控制檯屏幕和文本文件。我有點卡住了,希望有人能幫忙。我有一些幫助與Main()來解析我的文件,並設置了一個類來引導我通過,但仍然不知道如何獲取信息到屏幕和文本文件。我的代碼如下。C#語言控制檯應用程序

namespace ConsoleApp1 
{ 

class Program 
{ 
    static void Main(string[] args) 
    {   

    } 

    void Main() 
    { 
     var lines = ReadFile(); 

     lines.ToList().ForEach(Console.WriteLine); 
    } 

    IEnumerable<Line> ReadFile() 
    { 
     using (var reader = new StreamReader(File.OpenRead(@"C:List.txt"))) 
     { 
      const string directoryPrefix = " Directory of "; 
      Regex splittingRegex = new Regex(@"\s+", RegexOptions.Compiled); 
      string directory = null; 
      string line; 

      while ((line = reader.ReadLine()) != null) 
      { 
       line = line.TrimEnd(); 
       if (line.StartsWith(directoryPrefix)) 
       { 
        directory = line.Substring(directoryPrefix.Length); 
        continue; 
       } 

       var lineParts = splittingRegex.Split(line, 6); 
       yield return new Line { Date = lineParts[0], Time = lineParts[1], Period = lineParts[2], Bytes = lineParts[3], User = lineParts[4], Filename = Path.Combine(directory, lineParts[5]) }; 
      } 
     } 
    } 

    class Line 
    { 
     private string date; 
     private string time; 
     private string period; 
     private string bytes; 
     private string user; 
     private string filename; 

     public string Date { get { return date; } set { date = value; } } 
     public string Time { get { return time; } set { time = value; } } 
     public string Period { get { return period; } set { period = value; } } 
     public string Bytes { get { return bytes; } set { bytes = value; } } 
     public string User { get { return user; } set { user = value; } } 
     public string Filename { get { return filename; } set { filename = value; } } 
    } 
} 

}

+1

更改所有屬性類'Line' [自動屬性](http://msdn.microsoft.com/en-us /library/bb384054.aspx)並刪除支持字段。比如'public string Date {get; set;}'。 –

+0

@HighCore我同意這只是化妝品。 –

回答

0

你沒有做任何事情,當你輸出的每一行。在Line類

private void Main() 
    { 
     var lines = ReadFile().Select(l => l.ToString()).ToList(); 

     // The short way, do them after each other 
     lines.ForEach(Console.WriteLine); 
     File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines); 
    } 

並覆蓋toString()方法:你應該這樣做:

lines.ToList().ForEach(l => Console.WriteLine(l.User)); 
+0

當我替換這個lines.ToList()。ForEach(Console.WriteLine);與lines.ToList()。ForEach(l => Console.WriteLine(l.User));我仍然一無所獲。不知道問題出在哪裏。 –

1

只要改變Main()方法來此。

private class Line 
{ 
    public string Date { get; set; } 
    public string Time { get; set; } 
    public string Period { get; set; } 
    public string Bytes { get; set; } 
    public string User { get; set; } 
    public string Filename { get; set; } 

    public override string ToString() 
    { 
     // Just an example, you could create an other implementation 
     return string.Format("Filename: {0} - Date: {1}", Filename, Date); 
    } 
} 
+0

我在這條語句上收到錯誤File.WriteAllLines(@「C:\ WriteLines.txt」,lines);參數無效。 –

+0

@Alex,我認爲他的.net版本使用['string []'](http://msdn.microsoft.com/en-us/library/92e05ft3(v = vs.90).aspx)過載。 – gunr2171

+0

@ gunr2171你是對的!任何方式將其轉換爲字符串? –