4
Reply

Is it possible to use openFileDialog to get the folder path?

Rano AH

Rano AH

Sep 5 2013 4:10 AM
9.6k
Hello,

When I call string[] openFileDialog () function I can select file or multiple files and return them.  But is it possible to get also the path / directory? I mean is it possible to save the path of file so that I can pass it later to another function.

for example
If I declare globally static string oldPath;

and this is my function: 

private string[] GetOldFilePath()
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "TXT|*.txt";
            openFileDialog1.Multiselect = true;
           // openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {               
            string file = openFileDialog1.SafeFileName;

           foreach(string fileName in openFileDialog1.FileNames) 
                {
                    textBox1.Text += fileName + Environment.NewLine;
                }
                Files = openFileDialog1.FileNames; // it gets only the selected files

                string FolderPath = ???????????????? // is it possible to save here the path so  that I can assign this  value to the global oldPath variable. This way I  will use it later by pass it as a parameter to other function. What to  write to get the path of folder which contains the files?
            }
            return Files;
        }


Answers (4)