private static string[] openFolders(string directory, bool isAsync) { NativeMethods.BROWSEINFO bi = new NativeMethods.BROWSEINFO(); if (!string.IsNullOrEmpty(directory)) { _initialPath = Util.Helper.ValidatePath(directory); } IntPtr pidl = IntPtr.Zero; IntPtr bufferAddress = IntPtr.Zero; string folder = string.Empty; try { bufferAddress = System.Runtime.InteropServices.Marshal.AllocHGlobal(MAX_PATH_LENGTH); bi.dlgOwner = currentWindow; bi.pidlRoot = IntPtr.Zero; if (isAsync) { bi.ulFlags = BIF_SHAREABLE; } else { bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_SHAREABLE; } bi.lpfn = onBrowseEvent; bi.lParam = IntPtr.Zero; bi.iImage = 0; pidl = NativeMethods.SHBrowseForFolder(ref bi); if (NativeMethods.SHGetPathFromIDList(pidl, bufferAddress)) { folder = System.Runtime.InteropServices.Marshal.PtrToStringUni(bufferAddress); _initialPath = folder; } } catch (Exception ex) { Debug.LogError($"Folder dialog threw an error: {ex}"); } finally { if (bufferAddress != IntPtr.Zero) { System.Runtime.InteropServices.Marshal.FreeHGlobal(bufferAddress); } if (pidl != IntPtr.Zero) { System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pidl); } } return(new[] { folder }); }
public override string[] OpenFolders(string title, string directory, bool multiselect) { if (Util.Config.DEBUG && !string.IsNullOrEmpty(title)) { Debug.LogWarning("'title' is not supported under Windows."); } if (multiselect) { Debug.LogWarning("'multiselect' for folders is not supported under Windows."); } NativeMethods.BROWSEINFO bi = new NativeMethods.BROWSEINFO(); if (!string.IsNullOrEmpty(directory)) { _initialPath = Util.Helper.ValidatePath(directory); } IntPtr pidl = IntPtr.Zero; IntPtr bufferAddress = IntPtr.Zero; string folder = string.Empty; try { bufferAddress = System.Runtime.InteropServices.Marshal.AllocHGlobal(MAX_PATH_LENGTH); bi.dlgOwner = currentWindow; bi.pidlRoot = IntPtr.Zero; bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_SHAREABLE; bi.lpfn = onBrowseEvent; bi.lParam = IntPtr.Zero; bi.iImage = 0; pidl = NativeMethods.SHBrowseForFolder(ref bi); if (NativeMethods.SHGetPathFromIDList(pidl, bufferAddress)) { folder = System.Runtime.InteropServices.Marshal.PtrToStringUni(bufferAddress); _initialPath = folder; } } catch (Exception ex) { Debug.LogError(ex); } finally { if (bufferAddress != IntPtr.Zero) { System.Runtime.InteropServices.Marshal.FreeHGlobal(bufferAddress); } if (pidl != IntPtr.Zero) { System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pidl); } } return(new[] { folder }); }