1

I am trying to pull a text file called Teams.txt and have it displayed in a ListBox.
I have the Teams.txt inside the Visual Studio project file, but it is not reading it.

Does the file have to be in a certain place in order to be read?

private void BtnList_Click(object sender, EventArgs e)
{
    try
    {
        const int SIZE = 100;
        int[] numbers = new int[SIZE];
        int index = 0;
        StreamReader inputfile;
        inputfile = File.OpenText("Teams.txt");

        while (index < numbers.Length && !inputfile.EndOfStream)
        {
            numbers[index] = int.Parse(inputfile.ReadLine());
            index++;
        }
        inputfile.Close();

        foreach (int value in numbers)
        {
            lstTeams.Items.Add(value);
        }
    }
    catch
    {
        MessageBox.Show("Error.");
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • You should double check in VS that the [CopyToOutputDirectory] property is set to [Always] for the [Teams.txt] file. By default it was probably set to [Do Not Copy] and so is not in bin directory. – Neil.Work Oct 23 '18 at 22:19
  • @Neil.Work How may I check that? – Blake Bratu Oct 23 '18 at 22:24
  • If you want to edit the .csproj file you can do what TheGeneral said. Otherwise if you click on the file in the Solution Explorer, there is a properties window for the selected file. Generally that window is directly below the Solution Explorer. – Neil.Work Oct 23 '18 at 23:22

0 Answers0