public static string getFileName() { OFN ofn = new OFN() { lStructSize = Marshal.SizeOf <OFN>(), lpstrFilter = "All supported files\0*.mp4;*.mpeg4;*.mkv\0MPEG-4 part 12 files\0*.mp4;*.mpeg4\0Matroska multimedia files\0*.mkv\0\0", lpstrTitle = "Pick a media file to play", Flags = eFlags.Explorer | eFlags.FileMustExist | eFlags.PathMustExist, }; ofn.nMaxFile = MAX_PATH; Span <char> buffer = stackalloc char[MAX_PATH + 1]; buffer[MAX_PATH] = '\0'; unsafe { fixed(char *ptr = buffer) { ofn.lpstrFile = (IntPtr)ptr; bool picked = GetOpenFileNameW(ref ofn); if (!picked) { return(null); } } } int length = buffer.IndexOf('\0'); return(new string( buffer.Slice(0, length))); }
static extern bool GetOpenFileNameW([In] ref OFN ofn);