How to get filenames using OpenFileDialog in .NET (1000+ file multiselect)

Perhaps some of you have met this before ....

I open files for parsing. Of course I am using OpenFileDialog, but I am limited to 2048 buffer in the .FileNames line. This way I can only select a few hundred files. This is normal in most cases. However, for example, I have in one case 1400 files to open. You know a way to do this with an open file dialog. I just need an array of strings from .FileNames, I am passing this to the parser class.

I also thought about suggesting the FolderBrowserDialog option, and then I would use some other method to just loop through all the files in the directory, such as the DirectoryInfo class. I will do this as a last resort, if I don't have just one solution.

+2


source to share


4 answers


What I ended up with was writing a method that uses OpenFileDialog but indirectly checks the path string length. That is, if the method fails, the user is presented with an error indicating there are too many files, and then the FolderBrowser folder is displayed with the folder you selected the user was in. I also added separate options for importing files or importing a folder in the menu bar.

Here is the code for that. These are the methods in the static DataFileIO class where I put all I / O settings for writing to Excel or for access or xml, etc.



        public static string[] GetFiles()
    {
        string[] fileNames;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = UniversalDataImporter.Properties.Settings.Default.openFilePath;
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = false;
        openFileDialog1.Multiselect = true;
        openFileDialog1.CheckFileExists = false;

        try
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK && openFileDialog1.FileNames.Count() <501 )
            {
                UniversalDataImporter.Properties.Settings.Default.openFilePath =
                    Path.GetDirectoryName(openFileDialog1.FileName);
                UniversalDataImporter.Properties.Settings.Default.Save();
                return fileNames = openFileDialog1.FileNames;
            }
            else if (result == DialogResult.Cancel)
            {
                return null;
            }
            else
            {
                if (MessageBox.Show("Too many files were Selected. Would you like to import a folder instead?",
                    "Too many files...", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    return fileNames = GetFilesInFolder();
                }
                else
                {
                    return null;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            return null;
        }
    }

    public static string[] GetFilesInFolder()
    {

        FileInfo[] fileInfo;

        string pathName;
        FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

        folderBrowserDialog.RootFolder = System.Environment.SpecialFolder.Desktop;

        DialogResult results = folderBrowserDialog.ShowDialog();

        if (results == DialogResult.OK)
        {
            try
            {
                pathName = folderBrowserDialog.SelectedPath;

                DirectoryInfo dir = new DirectoryInfo(pathName);
                if (dir.Exists)
                {

                    fileInfo = dir.GetFiles();

                    string[] fileNames = new string[fileInfo.Length];

                    for (int i = 0; i < fileInfo.Length; i++)//this is shit
                    {
                        fileNames[i] = fileInfo[i].FullName;
                    }

                    return fileNames;
                }
                else
                {
                    return null;
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                return null;
            }

        }
        else if (results == DialogResult.Cancel) 
        {
            return null;
        }
        else { return null; }
    }

      

+1


source


my heck, I can't imagine how to select 1400 files in the open file dialog. Perhaps you should just let the user enter the filter and then make the call to System.IO.Directory.GetFiles.



+1


source


I would choose the FolderBrowser route. I would NEVER want to select 50-100 much less than 1000+ files manually. Better get a folder, ask for some pattern to match and select them that way. From a usability standpoint, selecting a large number of files is a bad choice IMHO.

+1


source


Do you have any errors or exceptions? Are you sure you are using OpenFileDialog

from namespace System.Windows.Forms

?

The following code works fine with over 2000 selected files:

System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
ofd.InitialDirectory = @"C:\Windows\system32\";
ofd.Multiselect = true;
ofd.ShowDialog();

foreach (var file in ofd.FileNames)
{
    Trace.WriteLine(file);
}

      

+1


source







All Articles