c#在打开文件对话框中有选择文件的数量限制吗?

本文关键字:文件 有选择 对话框 | 更新日期: 2023-09-27 18:16:08

我有一个c# windows窗体应用程序,我从打开文件浏览器加载XML文件和CGM图形文件到我的应用程序。我似乎可以一次选择几百个,它的工作没有故障,但任何更多和一个对话框弹出告诉我它找不到这样的文件,但只给出了一半的文件名。我假设这是由于可以通过打开文件对话框一次选择/处理的文件数量受到限制。

有人知道这个数字是多少吗?如果我一次选择的数量超过了这个限制,有没有办法解决这个问题?

我正在有效地将文件"导入"到我的应用程序中,因此使用foreach循环将选定的文件移动到另一个文件夹,并且应用程序将导入文件的所有文件名(以及文件上的其他数据)写入XML文件。

下面是整个'import'方法

private void addFilesToCSDBToolStripMenuItem_Click(object sender, EventArgs e)
{
    int DMsimported = 0;
    int graphicsImported = 0;
    if (projectName == "")
    {
        MessageBox.Show("Please open a project first", "DAWS");
        return;
    }
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
        MessageBox.Show("This process may take several minutes depending on the number of imports", "DAWS");
        Application.UseWaitCursor = true;
        foreach (string file in openFileDialog1.FileNames)
        {
            string fileName = Path.GetFileNameWithoutExtension(file); //Gets just the name from the file path
            string ext = Path.GetExtension(file.ToLower());
            if (ext != ".CGM" && ext != ".cgm")
            {
                    bool exists = xmlFileWriter.checkIfFIleExists(fileName + ext);
                    if (exists != true)
                    {
                        xmlFileWriter.writeDatatoXML(file);
                        File.Move(file, CSDBpath + projectName + "''CheckedIN''" + fileName + ext);
                        DMsimported = DMsimported + 1;
                    }
                    else
                    {
                        MessageBox.Show(fileName + " already exists in the CSDB. This file will be skipped.", "DAWS");
                    }  
                }
            else
            {
                if (File.Exists(CSDBpath + projectName + "''Graphics''" + fileName + ext))
                {
                    if (Properties.Settings.Default.OverwriteGraphics == true)
                    {
                        File.SetAttributes(CSDBpath + projectName + "''Graphics''" + fileName + ext, FileAttributes.Normal); // need this line in order to set the file attributes. Exception thrown otherwise when system tries to overwrite the file.
                        File.Delete(CSDBpath + projectName + "''Graphics''" + fileName + ext);
                        File.Copy(file, CSDBpath + projectName + "''Graphics''" + fileName  + ext); //need to give the option as to whether to delete the existing file or skipp.
                    }
                    else
                    {
                        MessageBox.Show(fileName + " already exists in the CSDB. This file will be skipped. To enable overwriting tick the checkbox in Preferences", "DAWS");
                    }
                }
                else
                {
                    File.Copy(file, CSDBpath + projectName + "''Graphics''" + fileName + ext);
                }
                graphicsImported = graphicsImported + 1;                            
            }   
        }
        Application.UseWaitCursor = false;
        buildAllListViews();
        copyCGMfilesToDirectories();
        if (DMsimported > 0)
        {
            MessageBox.Show(DMsimported.ToString() + " DM files successfully imported into the CSDB", "DAWS");
        }
        if (graphicsImported > 0)
        {
            MessageBox.Show(graphicsImported.ToString() + " graphic files successfully imported into the CSDB", "DAWS");
        }
        if (graphicsImported == 0 && DMsimported == 0)
        {
            MessageBox.Show("No files imported", "DAWS");
        }
        updateMainFilesList();  
    }  
}

c#在打开文件对话框中有选择文件的数量限制吗?

根据本文,当您使用OpenFileDialog控件选择超过200的文件时,您将收到" Too many files selected "错误消息。

刚刚在。net 4.5中进行了测试,5000个文件没有问题,所以看起来它取决于。net框架/os版本(我已经使用了足够长的文件名,只是为了确定,它不依赖于一些旧的windows约束,如所有文件名的最大长度是65536或32768):

var directory = @"c:'test'test";
Directory.CreateDirectory(directory);
for (int i = 0; i < 5000; i++)
{
    var path = Path.Combine(directory, i.ToString() + new string('a', 200));
    File.WriteAllText(path, "");
}
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    var fileCount = openFileDialog1.FileNames;
    var lastFileName = openFileDialog1.FileNames[4999];
}

在。net Framework 1.1中,对于OpenFileDialog。多选属性:

可以打开200个文件的硬编码限制打开文件对话框。有关此限制的详细信息,请参见第820631条"PRB: 'Too Many Files Selected'错误消息发生当你使用OpenFileDialog控件",在微软知识基地在http://support.microsoft.com.

当你必须处理这么多文件时,也许只选择文件夹更有意义(如果你选择文件夹中的所有文件更合理,如果这是你的情况)。尝试使用FolderBrowserDialog类:

var folderBrowserDialog = new FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
    var fi = new DirectoryInfo(folderBrowserDialog.SelectedPath);
    // here you get the files collection
    var files = fi.GetFiles();
}

尝试:

   if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    openFileDialog1.Multiselect = false;
    var fileCount = openFileDialog1.FileNames;
    var lastFileName = openFileDialog1.FileNames[4999];
}