如何在.NET中使用OpenFileDialog获取文件名(1000+文件多选)

发布于 2024-08-27 21:57:32 字数 325 浏览 3 评论 0原文

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

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

我还考虑提供一个FolderBrowserDialog 选项,然后使用其他一些方法来循环访问目录中的所有文件,例如DirectoryInfo 类。如果我无法拥有一体化的解决方案,我会将其作为最后的手段。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

℡寂寞咖啡 2024-09-03 21:57:33

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

这是执行此操作的代码。这些是名为 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; }
    }

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; }
    }
长亭外,古道边 2024-09-03 21:57:33

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

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.

不一样的天空 2024-09-03 21:57:33

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

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.

我不是你的备胎 2024-09-03 21:57:33

您收到任何错误或异常吗?您确定正在使用 System.Windows.Forms 命名空间中的 OpenFileDialog 吗?

以下代码可以完美地处理所选的 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文