shanon doh
shanon doh

Reputation: 41

How can extract from OpenFileDialog the files path name?

private void addGifsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog theDialog = new OpenFileDialog();
            theDialog.Title = "Add Gif Files";
            theDialog.Filter = "GIF files|*.gif";
            theDialog.InitialDirectory = @"C:\";
            theDialog.Multiselect = true;
            if (theDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string[] files = theDialog.SafeFileNames;
                    allfiles = new List<string>(files);
                    label2.Text = allfiles.Count.ToString();
                    if (allfiles.Count > 1)
                    {
                        button2.Enabled = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
            else
            {
                allfiles = new List<string>();
                label2.Text = "0";
            }
        }

All the added files from the same directory i need somehow to get the files directory name each time i click OK

After the line: label2.Text = "0"; to get the files directory path.

Upvotes: 1

Views: 54

Answers (1)

username
username

Reputation: 299

You can use Path.GetDirectoryName(filePath); to get the name of the directory for any given file path:

string directoryName = Path.GetDirectoryName(theDialog.FileName);

or:

foreach(string file in theDialog.FileNames)
{
    directoryNameList.Add(Path.GetDirectoryName(file));
}

Upvotes: 1

Related Questions