-1

I am working on a project with winforms. I want to read from an existing .txt file but without hard coding. This means that I want to allow the user to provide the path of the file as input. I have tried the following code:

private void pathSearch_TextChanged(object sender, EventArgs e)
{
    string path = "";
    OpenFileDialog ofd = new OpenFileDialog();

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        path = ofd.FileName;
        pathSearch.Text = File.ReadAllText(path);
    }

    string[] lines = File.ReadAllLines(path);

    if (!File.Exists(path))
    {
        // Create a file to write to.
        string[] createText = { "Hello", "And", "Welcome" };
        File.WriteAllLines(path, createText);
    }

    string[] values;

    for (int i = 0; i < lines.Length; i++)
    {
        values = lines[i].ToString().Split('|');
        string[] row = new string[values.Length];

        for (int j = 0; j < values.Length; j++)
        {
            row[j] = values[j].Trim();
        }
        table.Rows.Add(row);
    }
}

You can check screenshot1 and screenshot2 to give you an idea of how it will work. I want to enter the path inside the text field but it gives me an ArgumentNullException. After entering the path I want to import by using the button to see the data on that table. P.s I'm not sure if checking the existence of the path, works that way.

Edit
After the comments I edited my code and I have something that works. However, in order to import the file I have to select it 2 times and it is writing the same lines twice inside the table. How can I fix that?

Stefan
  • 652
  • 5
  • 19
  • 4
    This is not a Console application; `string path = Console.ReadLine();` won't help. Use the `Button.Click` event, not the `TextBox.TextChanged` event. You'ld need the `pathSearch.Text` property value. Use the `OpenFileDialog` class to create a standard open dialog to select a file. `File.Exists(path) => ` this condition will be verified in the OpenFileDialog. But, if you check that out yourself, do it before `File.ReadAllLines(path);`, otherwise it's quite useless. etc. – Jimi Oct 22 '19 at 23:34
  • 1
    [`Console.ReadLine()`](https://learn.microsoft.com/en-us/dotnet/api/system.console.readline) reads input from the console window. You need to use the text from your `TextBox` instead. Or better yet, implement a [`OpenFileDialog`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.openfiledialog) – Herohtar Oct 22 '19 at 23:34
  • You edit the `Text` property of `pathsearch` inside the `TextChanged` method, which will trigger this event again. – Stefan Oct 23 '19 at 06:27

1 Answers1

3

The problem is that you're doing this in the TextChanged event of the pathSearch text box, and you're modifying the text inside that event, so the event gets fired twice:

// This line of code causes the pathSearch_TextChanged event to fire again
pathSearch.Text = File.ReadAllText(path);

As a side note: do you really mean to write the contents of the file to the Text property of pathSearch, or should it just be the FileName instead?


Probably the best solution is to not modify the contents of a control inside an event that gets triggered when the contents are modified. Normally the OpenFileDialog is launched from a Button_Click event:

// Launch the file browser dialog from the click event of the Import button
private void btnImport_Click(object sender, EventArgs e)
{       
    OpenFileDialog ofd = new OpenFileDialog();
    if (ofd.ShowDialog() != DialogResult.OK) return;

    var path = ofd.FileName;
    pathSearch.Text = path;

    // Code continues...
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43