Search code examples
c#openfiledialog

How to read a filepath from a openfiledialog to an array of the containing file


I want to use 2 different buttons. One button to open an openfiledialog for the user to select a certain file, then they can click on the generate button to use the file selected to add all its contents to an array.

How do i get that file path to work in the System.IO.ReadAllLines(filepath)

i have this code for the open file dialog :

private void btnChoose_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog1.Filter = "Text Files (.txt)|*.txt";
    openFileDialog1.FilterIndex = 1;
    openFileDialog1.Multiselect = true;


    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        string sFileName = openFileDialog.FileName;
        string[] arrAllFiles = openFileDialog.FileNames;  
        txtScriptfile.Text = arrAllFiles[0];
    }

        }

And here i want to use the file path in the read all lines :

private void btnGenerate_Click(object sender, EventArgs e)
{

    string sThirdID = "";
    string sSiteName = "";
    string sFile = "";
    FilePathScript = "@\"" + txtScriptfile.Text + "\"";
    FilePathSite = "@\"" + txtSiteFile.Text +"\"";
    FilePathDestinationFolder = "@\"" + txtdestinationFolder.Text + "\"";
    txtScriptfile.Text = FilePathScript;
    txtSiteFile.Text = FilePathSite;
    txtdestinationFolder.Text = FilePathDestinationFolder;

    string[] Scriptlines = System.IO.File.ReadAllLines(FilePathScript);
    string[] Sitelines = System.IO.File.ReadAllLines(FilePathSite);

Solution

  • You want one file, not multiple, so drop the Multiselect=true or set Multiselect=false. With a single select, you want openFileDialog.FileName stored in your text control ... clean up the rest of Choose_Click.

    Generate_Click is a bit of a mess in some ways, but is actually close to what you want: You do nothing with sThirdID, sSiteName, and sFile, so get rid of them. Your treatment of filenames is a bit confusing: when dealing with string literals, you have to indicate them with double-quotes front and back; you also have to take special steps to deal with or "escape" backslashes, unless you use the special notation of preceding the entire string literal with a @ character, indicating a verbatim string. However, the filenames in your textboxes should have been set by the user selecting them from a system Open dialog, so there should be no extra treatment of them needed.

    So let's take the one filename for which you've provided the most detail and look at it, txtScriptFile.Text: if you pass this filename straight into File.ReadAllLines(), it should return the array of strings just as you have it written ... provided the user selected a valid text file when the program was run (e.g., one of the source code files for this project, for example).

    So the next thing would be, what are you to do with the array of strings once you have the file loaded? It's going to be hard to verify that you succeeded at the task without showing your results from the program somehow.