我試圖將我的數據輸出到控制檯屏幕和文本文件。我有點卡住了,希望有人能幫忙。我有一些幫助與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; } }
}
}
}
更改所有屬性類'Line' [自動屬性](http://msdn.microsoft.com/en-us /library/bb384054.aspx)並刪除支持字段。比如'public string Date {get; set;}'。 –
@HighCore我同意這只是化妝品。 –