views:

550

answers:

4

Maybe some of you have come across this before....

I am opening files for parsing. I'm using OpenFileDialog, of course, but i'm limited to a buffer of 2048 on the .FileNames string. Thus, I can only select a few hundred files. This is OK for most cases. However, fore example, I have in one case 1400 files to open. Do you know a way to do this with the open file dialog. I just want the string array of .FileNames, I pass that to parser class.

I was also thinking of offering a FolderBrowserDialog option and then I'd use some other method to just loop through all the files in a directory, like the DirectoryInfo class. I'd do this as a last resort if I can't have an all in one solution.

+1  A: 

my gosh I can't imagine selecting 1400 files in a file open dialog. Perhaps you should just allow the user to key in a filter and then do a System.IO.Directory.GetFiles call.

gbogumil
Pick a directory, pull in all file names, let the user filter or manually add/remove more single files or directories. This is a better solution than taxing the OFD and offers the user more flexibility.
Will
+1  A: 

I'd definitely go the FolderBrowser route. I'd NEVER want to have to select 50-100 much less 1000+ files manually. Better to retrieve the folder, prompt for some pattern to match and select them that way. From a usability standpoint, choosing a large number of files is a bad choice IMHO.

itsmatt
+1  A: 

Do you get any error or exception? Are you certain that you are using the OpenFileDialogfrom the System.Windows.Forms namespace?

The following code works perfectly with more than 2000 files selected:

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);
}
0xA3
A: 

What I ended up doing was writing a method uses the OpenFileDialog, but checks for the length of the path string indirectly. That is if the method fails, an error is displayed to the user telling them that there are too many files, and then a FolderBrowser is shown with the folder selected that the user was looking in. I also added seperate options to import files or import folders in the menubar.

Here is the code to do that. These are Methods in a static class called DataFileIO where I put all the customize IO stuff for writing to excel or 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; }
    }
Cole