Language/C#(CLR,.NET)
[C#] 오픈다이얼로그(OpenFileDialog)에서 다중선택(Multiselect)시 1000개 이상 파일을 가져오지 못하는 경우
천일몽
2012. 3. 8. 10:19
닷넷으로 오픈다이얼로그 박스를 이용한 다중 선택하여 1000개 이상의 파일을 가져오는 경우 XP에서 오류가 발생 되는걸 확인 (windows 7 64bit에서는 오류 없음)
그러나 델파이(정확히는 Lazarus)에서는 오류가 발생되지 않은것으로 보아 닷넷프레임워크나 CLR에서 오류가 발생되는것으로 판단되어 짐
역시 구글링으로 인한 해결 방법을 찾았으나 이것은 오류가 발생시 해당 폴더 전체 파일리스트를 가져오게끔 예외 처리를 하였음
그럼 소스 참고하시기 바랍니다.
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; }
}
이 글은 스프링노트에서 작성되었습니다.