Skip to content

Commit

Permalink
Added openfiledialog overload with defaultFilename
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Mar 19, 2021
1 parent 4cb279e commit 26984af
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Shared.Core/Utilities/OpenFileDialog.cs
Expand Up @@ -24,6 +24,12 @@ public class OpenFileDialog
/// </summary>
public const OpenSaveFileDialgueFlags MultiFileFlags = SingleFileFlags | OpenSaveFileDialgueFlags.OFN_ALLOWMULTISELECT;

/// <inheritdoc cref="ShowDialog(string,string,string,string,OpenSaveFileDialgueFlags,string,IntPtr)"/>
public static string[] ShowDialog(string title, string initialDir, string filter, string defaultExt, OpenSaveFileDialgueFlags flags, IntPtr owner = default)
{
return ShowDialog(title, initialDir, filter, defaultExt, flags, null, owner);
}

/// <summary>
/// Show windows file open dialog. Blocks the thread until user closes the dialog. Returns list of selected files, or null if user cancelled the action.
/// </summary>
Expand All @@ -49,7 +55,8 @@ public class OpenFileDialog
/// This member can be a combination of the CommomDialgueFlags.
/// </param>
/// <param name="owner">Hwnd pointer of the owner window. IntPtr.Zero to use default parent</param>
public static string[] ShowDialog(string title, string initialDir, string filter, string defaultExt, OpenSaveFileDialgueFlags flags, IntPtr owner)
/// <param name="defaultFilename"> Filename that is initially entered in the filename box. </param>
public static string[] ShowDialog(string title, string initialDir, string filter, string defaultExt, OpenSaveFileDialgueFlags flags, string defaultFilename, IntPtr owner = default)
{
const int MAX_FILE_LENGTH = 2048;

Expand All @@ -62,12 +69,17 @@ public static string[] ShowDialog(string title, string initialDir, string filter
ofn.initialDir = initialDir;
ofn.title = title;
ofn.flags = (int)flags;
if (defaultExt != null) ofn.defExt = defaultExt;

// Create buffer for file names
var fileNames = new String(new char[MAX_FILE_LENGTH]);
var fileNamesArr = new char[MAX_FILE_LENGTH];
defaultFilename?.CopyTo(0, fileNamesArr, 0, defaultFilename.Length);
var fileNames = new String(fileNamesArr);
ofn.file = Marshal.StringToBSTR(fileNames);
ofn.maxFile = fileNames.Length;

if (owner == default)
owner = NativeMethods.GetActiveWindow();
ofn.dlgOwner = owner;

// Save and restore working directory after GetOpenFileName changes it
Expand Down

0 comments on commit 26984af

Please sign in to comment.