Reputation: 437
I need to get the selected files in a OpenFileDialog. For a single file, I'm doing this:
EDIT:
var count = SelectedItems(handle); // handle is the handle to ListView control in OpenFiledialog
var bufferSize = 2048 * count;
if (bufferSize > 0)
{
var path = new StringBuilder(bufferSize)
SendMessage(handle, CDM_GETFILEPATH, (IntPtr)path.Capacity, path);
}
And it works fine, but I need to check for multiple files (Multiselect = true). I don't know the buffer size (stringBuilder size).
EDIT: I'm customizing the Open File Dialog. I'm trying to get the selected files before the OFD window is closed.
EDIT: Currently I can have a list of selected files, BUT, only the filenames, without path, I'm using this:
var totalItemsCount = SendMessage(handle, LVM_GETITEMCOUNT, 0, 0);
var fileNames = new List<string>();
var lvi = new LVITEM();
var lviPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LVITEM)));
for (var i = 0; i < totalItemsCount; i++)
{
var pcsText = Marshal.AllocHGlobal(1024);
lvi.iSubItem = 0;
lvi.cchTextMax = 1024;
lvi.pszText = pcsText;
Marshal.StructureToPtr(lvi, lviPtr, fDeleteOld: true);
var success = SendMessage(handle, LVM_GETITEMTEXT, i, (int)lviPtr);
if (success > 0)
{
var itemState = SendMessage(handle, LVM_GETITEMSTATE, i, 2);
var selected = (itemState & 2) != 0;
if (selected = (itemState & 2) != 0)
{
lvi = (LVITEM)Marshal.PtrToStructure(lviPtr, typeof(LVITEM));
var name = Marshal.PtrToStringAnsi(lvi.pszText);
fileNames.Add(name);
}
}
Marshal.FreeHGlobal(pcsText);
}
Marshal.FreeHGlobal(lviPtr);
What I'm doing is to get the selected files directly from the listview, but I need gthe filenames with the path.
When the OpenfileDialog change the folder, I can get the folder path with:
var folderPath = new StringBuilder(256);
SendMessage(handle, CDM_GETFOLDERPATH, (IntPtr)256, folderPath);
But it doesnt work when the folder is an special folder, like the libraries.
Upvotes: 0
Views: 2036
Reputation: 5738
Simple :
IO.Stream st1
///Set MultiSelect property to true
openfiledialog1.MultiSelect = true;
///Now get the filenames
if (openfiledialog1.ShowDialog() == DialogResult.OK)
{
foreach (String file in openfiledialog1.FileNames)
{
try
{
if ((st1 = openfiledialog1.OpenFile()) != null)
{
using (st1)
{
List.Add(file);
}
}
}
Note: Don't know if this will also work with the custom control as well as no code is provided regarding the custom control
Upvotes: 0