GetOpenFileName for multiple files

  • Thread starter Thread starter thereal_floppes
  • Start date Start date
T

thereal_floppes

Guest
I am trying to use the Win32 API GetOpenFileName() to display the standard "Open" dialog and to let the user select multiple files. I know that .NET has its own managed class for this, but it does not display the "new" dialog style in Vista and Win 7. Only with SP1 for both .NET 2.0 and .NET 3.0 there is a new property FileDialog.AutoUpgradeEnabled which would apply the new style, but my app needs to work with Vista in its original configuration with .NET 3.0 without SP1.

Sooo, I got GetOpenFileName() working when I let the user select only one file. But I need him to select multiple files and this causes problems. Here's my code first:


[DllImport("comdlg32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool GetOpenFileName([In, Out] OpenFileName ofn);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private class OpenFileName {
public int structSize = 0;
public IntPtr dlgOwner = IntPtr.Zero;
public IntPtr instance = IntPtr.Zero;
public string filter;
public string customFilter;
public int maxCustFilter = 0;
public int filterIndex = 0;
public string file;
public int maxFile = 0;
public string fileTitle;
public int maxFileTitle = 0;
public string initialDir;
public string title;
public int flags = 0;
public short fileOffset = 0;
public short fileExtension = 0;
public string defExt;
public IntPtr custData = IntPtr.Zero;
public IntPtr hook = IntPtr.Zero;
public string templateName;
public IntPtr reservedPtr = IntPtr.Zero;
public int reservedInt = 0;
public int flagsEx = 0;
}

private enum OpenFileNameFlags {
OFN_HIDEREADONLY = 0x4,
OFN_FORCESHOWHIDDEN = 0x10000000,
OFN_ALLOWMULTISELECT = 0x200,
OFN_EXPLORER = 0x80000
}

public string[] ShowOpenFileDialog(string dialogTitle, string startPath, string filter, bool showHidden) {
OpenFileName ofn = new OpenFileName();

ofn.structSize = Marshal.SizeOf(ofn);
ofn.filter = filter.Replace("|", "\0") + "\0";
ofn.file = new String(new char[1024]);
ofn.maxFile = ofn.file.Length;
ofn.fileTitle = new String(new char[1024]);
ofn.maxFileTitle = ofn.fileTitle.Length;
ofn.initialDir = startPath;
ofn.title = dialogTitle;
ofn.flags = (int)OpenFileNameFlags.OFN_HIDEREADONLY | (int)OpenFileNameFlags.OFN_ALLOWMULTISELECT | (int)OpenFileNameFlags.OFN_EXPLORER;

if (showHidden) {
ofn.flags = ofn.flags | (int)OpenFileNameFlags.OFN_FORCESHOWHIDDEN;
}

if (GetOpenFileName(ofn)) {
string[] selectedFiles = ofn.file.Split(new char[] { '\0' });

return selectedFiles;
} else {
return null;
}
}
Try the code with this call:

string[] files = ShowOpenFileDialog("Multiple Files", "", "All Files (*.*)|*.*", false);

oftn.file will contain the directory name and then the selected file names, all separated by a NULL byte. My problem is that Interop seems to cut off the string at the first NULL byte, thus returning me only the directory name. The file names get cut off. How can I avoid this?

Continue reading...
 
Back
Top