|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Multi File Dialogue
Hi,
How do you retrieve the different properties of multiple files using OpenFileDialog? E.G the following code gets the FileName property for each selected file Code:
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
OpenFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (String fileName in openFileDialog1.FileNames)
{
// process fileName
}
}
But how would I get the FileName and SafeFileName for each selected file? Thanks for your help! |
|
#2
|
||||
|
||||
|
If by safe you mean the short FAT file system compatible version; there is a Win32 API that will return the short path/file when given a full NTFS path/file. I don't recall what it is off of the top of my head and I am sure there's a .NET equivalent for it. Does your File Dialog even show the short file names? I kind of doubt that it would. There's no reason for it to be an attribute of the file. Pretty sure there's just a simple algorithm they apply to a real path/file name to come up with the short one.
__________________
My worst nightmare was a pointless infinite loop. Work in progress; don't poke the curmudgeon! http://www.odonahue.com/ |
|
#3
|
||||
|
||||
|
You should find some answers here:
http://msdn.microsoft.com/en-us/lib...247(VS.85).aspx http://msdn.microsoft.com/en-us/lib...989(VS.85).aspx I am still investigating whether there is a pure .NET solution for you. |
|
#4
|
|||
|
|||
|
Hi jwdonahue,
Thanks for your help! In this case I am looking for the File Path and File Name. These can be retrieved from the openFileDialog object properties ie. String fileName = openFileDialog1.SafeFileName String fileNameWithPath = openFileDialog1.FileName This works when one file is selected. When 2 or more files are selected you use openFileDialog1.SafeFileNames and openFileDialog1.FileNames which return string arrays. The following code works to process both string arrays. However I am sure there must be a much more elegant way to get the same result and avoid the 3 different loops?? Please let me know if you have any thoughts. Code:
private void btnAddFile_Click(object sender, EventArgs e)
{
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
OpenFileDialog1.Multiselect = true;
// Add Selected Files from the OpenFileDialog
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
int loopCounter = 0;
int intNumberFilesSelected = openFileDialog1.FileNames.GetLength(0);
String[] fileNames = new String[intNumberFilesSelected];
String[] safeFileNames = new String[intNumberFilesSelected];
// Retrieve File Name Including File Path
foreach (String fileName in openFileDialog1.FileNames)
{
fileNames[loopCounter] = fileName;
loopCounter++;
}
loopCounter = 0;
// Retrieve File Name
foreach (String safeFileName in openFileDialog1.SafeFileNames)
{
safeFileNames[loopCounter] = safeFileName;
loopCounter++;
}
// Add Items to ListView
for (int i = 0; i < intNumberFilesSelected; i++)
{
ListViewItem lviFile = lvwFiles.Items.Add(safeFileNames[i]);
lviFile.SubItems.Add(fileNames[i]);
}
}
}
Thanks |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Multi File Dialogue |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|