F D
F D

Reputation: 23

C # language Console App

I'm trying to output my data to console screen and to a text file. I'm a bit stuck and was hoping someone can help. I have had some help with the Main() to parse my file and set me up with a Class to guide me through, but still not sure how to get the info to the screen and into a text file. My code is below.

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

}

Upvotes: 0

Views: 162

Answers (3)

Alex Siepman
Alex Siepman

Reputation: 2598

Just change the Main() method to this:

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

And override the ToString() Method in the Line class.

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

Upvotes: 1

Tarik
Tarik

Reputation: 11209

You could use the EchoStream from this open source project: http://www.codeproject.com/Articles/3922/EchoStream-An-Echo-Tee-Stream-for-NET

Upvotes: 0

David Hoerster
David Hoerster

Reputation: 28701

You're not doing anything when you output each line. You should do this:

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

Upvotes: 0

Related Questions