我有一个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();
}
}
答案 0 :(得分:3)
根据此article,当您使用Too many files selected
控件选择超过OpenFileDialog
个文件时,您会收到“200
”错误消息。
答案 1 :(得分:3)
刚刚在.NET 4.5中测试过,5000文件没问题,所以看起来它依赖于.net framework / 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];
}
答案 2 :(得分:1)
在.NET Framework 1.1中,OpenFileDialog.Multiselect Property:
有200个文件的硬编码限制,可以使用 打开文件对话框。有关此限制的详细信息,请参阅 文章820631,“PRB:'选择了太多文件'错误信息发生 当您使用OpenFileDialog控件时,在Microsoft知识中 以http://support.microsoft.com为基础。
当您需要使用如此大量的文件时,可能只选择文件夹更有意义(如果选择文件夹中的所有文件,则更合理,如果是这种情况)。尝试使用FolderBrowserDialog Class:
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();
}
答案 3 :(得分:0)
尝试:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
openFileDialog1.Multiselect = false;
var fileCount = openFileDialog1.FileNames;
var lastFileName = openFileDialog1.FileNames[4999];
}