2

也许你们中的一些人以前遇到过这种情况......

我正在打开文件进行解析。当然,我使用的是 OpenFileDialog,但我仅限于 .FileNames 字符串上的 2048 缓冲区。因此,我只能选择几百个文件。大多数情况下都可以。但是,例如,我在一种情况下要打开 1400 个文件。您知道使用打开文件对话框执行此操作的方法吗?我只想要.FileNames 的字符串数组,我将它传递给解析器类。

我还考虑提供一个 FolderBrowserDialog 选项,然后我会使用其他方法来遍历目录中的所有文件,例如 DirectoryInfo 类。如果我不能有一个多合一的解决方案,我会作为最后的手段这样做。

4

4 回答 4

1

天哪,我无法想象在文件打开对话框中选择 1400 个文件。也许您应该只允许用户键入过滤器,然后调用 System.IO.Directory.GetFiles。

于 2010-04-09T13:07:40.767 回答
1

我肯定会选择 FolderBrowser 路线。我永远不想手动选择 50-100 少得多的 1000 多个文件。最好检索文件夹,提示某些模式以匹配并以这种方式选择它们。从可用性的角度来看,选择大量文件是一个糟糕的选择恕我直言。

于 2010-04-09T13:09:53.233 回答
1

你有任何错误或异常吗?您确定您使用的是OpenFileDialog来自System.Windows.Forms命名空间吗?

以下代码完美适用于选择的 2000 多个文件:

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);
}
于 2010-04-09T13:14:37.227 回答
1

我最终做的是编写一个使用 OpenFileDialog 的方法,但间接检查路径字符串的长度。也就是说,如果该方法失败,则会向用户显示一个错误,告诉他们文件太多,然后显示一个 FolderBrowser,其中包含用户正在查看的所选文件夹。我还添加了单独的选项来导入文件或导入菜单栏中的文件夹。

这是执行此操作的代码。这些是名为 DataFileIO 的静态类中的方法,我将所有自定义 IO 内容用于写入 excel 或 access 或 xml 等。

        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; }
    }
于 2010-06-23T12:35:57.730 回答