Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 131,705 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,429 people online right now. Registration is fast and FREE... Join Now!




Write Console text to a variable

 
Reply to this topicStart new topic

Write Console text to a variable

ragingben
post 7 Oct, 2008 - 03:21 AM
Post #1


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 30


My Contributions


Hi there, I'm creating a forms application that has a front end (the forms stuff) and controls a back end (external controller). That is not overly important - the problem I am having is that I want to be able to read all the debug comments that I have written to the console with Console.WriteLine into a List with the generic type String so that I can then pass them to another method to format, and eventually save as a text file whenever an error occurs.

Here is the method that will do this, am I on the right track?

CODE

        private List<String> frontEndLog()
        {
            List<String> data = new List<String>();

            // not working - should read console to a variable

            StreamReader sr = new StreamReader(Console.OpenStandardInput(100));
            using (sr)
            {
                String line = null;
                do
                {
                    // want to read the lines of sr here
                    data.Add(line);
                }
                while (line != null);
            }

            return data;
        }


Many thanks in advance.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 7 Oct, 2008 - 03:25 AM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,377



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


I think this post might help.

His code is in VB, but it's easily translatable.

smile.gif
User is offlineProfile CardPM

Go to the top of the page

ragingben
post 7 Oct, 2008 - 03:48 AM
Post #3


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 30


My Contributions


Thanks a lot, that does make a bit of sense although I'm not quite sure how to implement that yet, but I'm sure it will all fit into place!

I'm suprised that there is no method for the Console object that returns all it's text output though, I thought that this would be a lot simpilar than it is!
User is offlineProfile CardPM

Go to the top of the page

ragingben
post 7 Oct, 2008 - 04:06 AM
Post #4


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 30


My Contributions


Sorry gabeHabe (or anyone else for that matter!) but I have sorted the code, but am unsure about what path to pass the method. Would it be the debug exe?

Sorry, I'm having a mental block biggrin.gif

CODE

/// <summary>
        /// Gather Front End Log data
        /// </summary>
        /// <returns></returns>
        private List<String> frontEndLog()
        {
            List<String> data = new List<String>();
            String _Path = Directory.GetCurrentDirectory().ToString();

            // Start a new process
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            // redirect standard output
            p.StartInfo.RedirectStandardOutput = true;

            // assign directory and filename ***
            p.StartInfo.WorkingDirectory = _Path.Substring(0, _Path.LastIndexOf("\\") + 1);
            p.StartInfo.FileName = p.StartInfo.WorkingDirectory + _Path.Substring(_Path.LastIndexOf("\\") + 1);

            // set options
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;

            // start process
            p.Start();
            // read standard output to end and put in element of List
            data.Add(p.StandardOutput.ReadToEnd());
            // wait for process to exit
            p.WaitForExit();
            // return data
            return data;
        }
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 09:21AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month