也许有些人之前遇到过这个......
我正在打开解析文件。我当然正在使用OpenFileDialog,但我只限于.FileNames字符串上的2048缓冲区。因此,我只能选择几百个文件。对大多数情况来说这是可以的。但是,例如,我在一个案例中打开了1400个文件。您是否知道使用打开文件对话框执行此操作的方法。我只想要.FileNames的字符串数组,我把它传递给解析器类。
我还想提供一个FolderBrowserDialog选项,然后我会使用其他方法来遍历目录中的所有文件,比如DirectoryInfo类。如果我不能拥有一体化的解决方案,我会把它作为最后的手段。
答案 0 :(得分:1)
我的天哪我无法想象在文件打开对话框中选择1400个文件。也许您应该只允许用户键入过滤器,然后执行System.IO.Directory.GetFiles调用。
答案 1 :(得分:1)
我肯定会去FolderBrowser路线。我绝不想手动选择50-100个少1000多个文件。最好检索文件夹,提示某些模式匹配并以这种方式选择它们。从可用性的角度来看,选择大量文件是一个糟糕的选择恕我直言。
答案 2 :(得分:1)
你有任何错误或例外吗?您确定使用OpenFileDialog
命名空间中的System.Windows.Forms
吗?
以下代码与所选的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);
}
答案 3 :(得分:1)
我最终做的是编写一个使用OpenFileDialog的方法,但间接检查路径字符串的长度。也就是说,如果该方法失败,则会向用户显示一条错误,告诉他们文件太多,然后会显示一个FolderBrowser,其中包含用户正在查看的文件夹。我还添加了单独的选项来导入文件或导入菜单栏中的文件夹。
这是执行此操作的代码。这些是名为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; }
}