3

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

有谁知道这个数字是多少,如果我一次选择超过这个限制,有没有办法解决它?

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

下面是整个“导入”方法

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();  
    }  
}
4

4 回答 4

3

根据这篇文章Too many files selected,当您使用OpenFileDialog控件选择多个200文件时,您将收到“ ”错误消息。

于 2013-05-31T08:47:37.813 回答
3

刚刚在 .NET 4.5 中测试,5000 个文件没有问题,所以它看起来取决于 .net 框架/操作系统版本(我使用了足够长的文件名,只是为了确保它不依赖于一些旧窗口约束,例如所有文件名的最大长度为 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];
}
于 2013-05-31T08:54:00.603 回答
1

在 .NET Framework 1.1 中,对于OpenFileDialog.Multiselect 属性

可以使用“打开文件”对话框打开的硬编码限制为 200 个文件。有关此限制的详细信息,请参阅 Microsoft 知识库http://support.microsoft.com中的文章 820631,“PRB:使用 OpenFileDialog 控件时出现'选择太多文件'错误消息” 。

当您必须处理如此大量的文件时,仅选择文件夹可能更有意义(如果您选择文件夹中的所有文件,如果是这种情况,则更合理)。尝试使用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();
}
于 2013-05-31T08:50:17.127 回答
0

尝试:

   if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    openFileDialog1.Multiselect = false;
    var fileCount = openFileDialog1.FileNames;
    var lastFileName = openFileDialog1.FileNames[4999];
} 
于 2013-05-31T09:27:06.500 回答