-1

Why doesn't my collection of strings (lst) appear in the console? Visual Studio does not show any obvious errors. Please point out my mistake.

public partial class Home : Page {
    public Home() {
        InitializeComponent();
    }

    // ...

    private void Button_Click(object sender, EventArgs e) {
        Home.ABC();
        MessageBox.Show("hello world");
    }

    static void ABC() {
        List<string> lst = new List<string>();
        OpenFileDialog opendialog = new OpenFileDialog();
        opendialog.Multiselect = true;
        bool? dialogResult = opendialog.ShowDialog();
        if (dialogResult.HasValue && dialogResult.Value) {
            foreach (var file in opendialog.Files) {
                Stream fileStream = file.OpenRead();
                using (StreamReader reader = new StreamReader(fileStream)) {
                    while (!reader.EndOfStream) {
                        string line=reader.ReadLine();
                        lst.Add(line);
                    }
                    Console.WriteLine(lst);
                }
            }
        }
    }
}

The message box shows hello world, so the function ABC() also runs?

enter image description here

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
user3214034
  • 221
  • 1
  • 6

3 Answers3

1

As mentioned above, you are not using a console application and as such you cannot write to the console.

Create a RichTextBox and use

foreach(var line in lst)
{
    RichTextBoxName.AppendText(line)
}

foreach(var line in lst)
    {
        RichTextBoxName.AppendText(line + "\n") //adds a new line after each string
    }
Inept Adept
  • 414
  • 4
  • 11
0

You cannot use a console window in a standard Winforms application. It is launched with Application.Run and shows the default dialog, rather than the console.

In order to display a console window AS WELL as a winforms dialog, you have to use Win32 API to manually create the console window.

See here:

How do I show a console output/window in a forms application?

Community
  • 1
  • 1
Jon Barker
  • 1,788
  • 15
  • 25
0

The console stream output isn't set to a visible console window, so Console.Write accepts the text, but doesn't display anything anywhere.

Try using Debug.WriteLine instead.