C#是有限制选定的文件数量在打开文件对话框 [英] C# is there a limit to number of selected files in Open File Dialog

查看:191
本文介绍了C#是有限制选定的文件数量在打开文件对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#Windows窗体应用程序,我的XML文件和图形CGM文件加载到从打开的文件浏览器我的申请。我似乎能够在同一时间选择了几百和它的作品没有过错,但更多的和一个对话框弹出,告诉我它无法找到文件,如此这般,但只给了一半的文件名。我假定这是由于在一个处理过的可以选择的文件/量的限制通过打开文件对话框。



有谁知道这是什么数字是,是有办法解决它,如果我有比限制更多选择一次?



我有效地进口的文件到我的应用程序,即使用foreach循环所选择的文件被移动到另一个文件夹和应用程序写入到一个XML文件,导入文件的所有文件名(以及其他数据的文件)。



下面是整个'进口'的方法。

 私人无效addFilesToCSDBToolStripMenuItem_Click(对象发件人,EventArgs五)
{
INT DMsimported = 0;
INT graphicsImported = 0;

如果(项目名称==)
{
MessageBox.Show(请首先打开一个项目,DAWS);
的回报;
}

DialogResult的结果= openFileDialog1.ShowDialog();
如果(结果== DialogResult.OK)
{
MessageBox.Show(这个过程可能需要依赖进口的数量几分钟,DAWS);
Application.UseWaitCursor = TRUE;

的foreach(在openFileDialog1.FileNames字符串文件)
{
字符串文件名= Path.GetFileNameWithoutExtension(文件); //从文件路径
串分机= Path.GetExtension获取只是名称(file.ToLower());

如果(EXT =.CGM&放大器;!&安培; EXT =的.cgm!)
{
布尔存在= xmlFileWriter.checkIfFIleExists(文件名+分机);

如果(存在=真!)
{
xmlFileWriter.writeDatatoXML(文件);
File.Move(文件,CSDBpath +项目名称+\\CheckedIN\\+文件名+分机);
DMsimported = DMsimported + 1;
}
,否则
{
MessageBox.Show(文件名+。在CSDB已经存在该文件将被跳过。,DAWS);
}
}
,否则
{
如果(File.Exists(CSDBpath +项目名+\\Graphics\\+文件名+分机) )
{
如果(Properties.Settings.Default.OverwriteGraphics ==真)
{
File.SetAttributes(CSDBpath +项目名+\\Graphics\\ +文件名+分机,FileAttributes.Normal); //需要这一行,以设置文件属性。否则异常抛出时系统尝试将覆盖该文件。
File.Delete(CSDBpath +项目名+\\Graphics\\+文件名+分机);
File.Copy(文件,CSDBpath +项目名称+\\Graphics\\+文件名+分机); //需要给选项为是否要删除现有的文件或skipp。
}
,否则
{
MessageBox.Show(文件名+的CSDB已经存在,该文件将被跳过。为了使覆盖在打勾首复选框,DAWS );
}
}
,否则
{
File.Copy(文件,CSDBpath +项目名称+\\Graphics\\+文件名+分机) ;
}

graphicsImported = graphicsImported + 1;
}
}

Application.UseWaitCursor = FALSE;

buildAllListViews();
copyCGMfilesToDirectories();

如果(DMsimported大于0)
{
MessageBox.Show(DMsimported.ToString(),DAWS+成功导入到CSDB DM文件);
}
如果(graphicsImported大于0)
{
MessageBox.Show(graphicsImported.ToString()+图形文件成功导入到CSDB,DAWS);
}
如果(graphicsImported == 0安培;&安培; DMsimported == 0)
{
MessageBox.Show(无文件中导入,DAWS);
}

updateMainFilesL​​ist();
}
}


解决方案

刚测试在.NET 4.5,没有问题的5000个文件,所以它看起来像它依赖于.NET框架/ OS版本(我用足够长文件名,就可以肯定,它不依赖于某些旧的Windows约束像所有的文件名的最大长度为65536或32768):

  var目录= @C:\test\测试; 
Directory.CreateDirectory(目录);

的for(int i = 0; I< 5000;我++)
{
VAR路径= Path.Combine(目录,i.ToString()+新的字符串(一个',200));
File.WriteAllText(路径,);
}

如果(openFileDialog1.ShowDialog()== DialogResult.OK)
{
VAR fileCount = openFileDialog1.FileNames;
VAR lastFileName = openFileDialog1.FileNames [4999];
}


I've got a C# windows forms application where I load XML files and CGM graphics files into my application from an Open File browser. I seem to be able to select a couple of hundred at a time and it works without fault, but any more and a dialog box pops up telling me it can't find file such and such, but only gives half the file name. I'm assuming this is due to a limit on the amount of files that can be selected / processed in one go through the open file dialog.

Does anybody know what that number is, and is there a way around it if i have more than that limit to select at once?

I'm effectively 'importing' the files into my application, whereby using a foreach loop the selected files get moved to another folder, and the application writes to an XML file with all the file names of the files imported (as well as other data on the files).

Below is the entire 'import' method

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

解决方案

Just tested in .NET 4.5, no problem with 5000 files, so it looks like it depends on .net framework/os version (I've used long enough file names, just to be sure, that it does not depend on some old windows constraint, like max length of all file names is 65536 or 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];
}

这篇关于C#是有限制选定的文件数量在打开文件对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆