0

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

}

2
  • 1
    Change all your properties in class Line to Auto Properties and remove the backing fields. such as public string Date {get;set;}.
    – Fede
    Aug 28, 2013 at 18:18
  • @HighCore I agree but that's just cosmetics. Aug 28, 2013 at 19:05

3 Answers 3

1

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);
    }
}
7
  • I am getting an error on this statement File.WriteAllLines(@"C:\WriteLines.txt", lines); Arguments are invalid.
    – F D
    Aug 28, 2013 at 18:43
  • @Alex, I think his version of .net uses the string[] overload.
    – gunr2171
    Aug 28, 2013 at 18:53
  • use lines.ToArray(). This will make it a string array, which should work.
    – gunr2171
    Aug 28, 2013 at 19:24
  • @gunr2171 is this what you meant -> File.WriteAllLines(@"C:\users\WriteLines.txt", lines.ToArray());
    – F D
    Aug 28, 2013 at 19:37
  • @FD, yes, did that work? (And sorry to Alex for hijacking the post)
    – gunr2171
    Aug 28, 2013 at 19:39
0

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

lines.ToList().ForEach(l => Console.WriteLine(l.User));
1
  • When I replace this lines.ToList().ForEach(Console.WriteLine); with lines.ToList().ForEach(l => Console.WriteLine(l.User)); I still get nothing. Not sure where the issue is.
    – F D
    Aug 28, 2013 at 18:33
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.