Skip to content

Commit

Permalink
Property Title added, coments added and code cleaned.
Browse files Browse the repository at this point in the history
  • Loading branch information
evaristocuesta committed Nov 5, 2020
1 parent 635ccae commit 9fae224
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 33 deletions.
45 changes: 32 additions & 13 deletions FolderBrowserEx/FolderBrowserDialog.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
Expand All @@ -11,6 +10,11 @@ namespace FolderBrowserEx
{
public class FolderBrowserDialog : IFolderBrowserDialog
{
/// <summary>
/// Gets/sets the title of the dialog
/// </summary>
public string Title { get; set; }

/// <summary>
/// Gets/sets folder in which dialog will be open.
/// </summary>
Expand All @@ -25,11 +29,27 @@ public class FolderBrowserDialog : IFolderBrowserDialog
/// <summary>
/// Gets selected folder.
/// </summary>
public string SelectedFolder { get; set; }
public string SelectedFolder { get; private set; }

/// <summary>
/// Shows the folder browser dialog with a the default owner
/// </summary>
/// System.Windows.Forms.DialogResult.OK if the user clicks OK in the dialog box;
/// otherwise, System.Windows.Forms.DialogResult.Cancel.
/// </returns>
public DialogResult ShowDialog()
{
return ShowDialog(owner: new WindowWrapper(GetHandleFromWindow(GetDefaultOwnerWindow())));
}

/// <summary>
/// Shows the folder browser dialog with a the specified owner
/// </summary>
/// <param name="owner">Any object that implements IWin32Window to own the folder browser dialog</param>
/// <returns>
/// System.Windows.Forms.DialogResult.OK if the user clicks OK in the dialog box;
/// otherwise, System.Windows.Forms.DialogResult.Cancel.
/// </returns>
public DialogResult ShowDialog(IWin32Window owner)
{
if (Environment.OSVersion.Version.Major >= 6)
Expand All @@ -44,8 +64,7 @@ public DialogResult ShowDialog(IWin32Window owner)
private DialogResult ShowVistaDialog(IWin32Window owner)
{
var frm = (NativeMethods.IFileDialog)(new NativeMethods.FileOpenDialogRCW());
uint options;
frm.GetOptions(out options);
frm.GetOptions(out uint options);
options |= NativeMethods.FOS_PICKFOLDERS |
NativeMethods.FOS_FORCEFILESYSTEM |
NativeMethods.FOS_NOVALIDATE |
Expand All @@ -54,35 +73,35 @@ private DialogResult ShowVistaDialog(IWin32Window owner)
frm.SetOptions(options);
if (this.InitialFolder != null)
{
NativeMethods.IShellItem directoryShellItem;
var riid = new Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"); //IShellItem
if (NativeMethods.SHCreateItemFromParsingName
(this.InitialFolder, IntPtr.Zero, ref riid,
out directoryShellItem) == NativeMethods.S_OK)
out NativeMethods.IShellItem directoryShellItem) == NativeMethods.S_OK)
{
frm.SetFolder(directoryShellItem);
}
}
if (this.DefaultFolder != null)
{
NativeMethods.IShellItem directoryShellItem;
var riid = new Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"); //IShellItem
if (NativeMethods.SHCreateItemFromParsingName
(this.DefaultFolder, IntPtr.Zero, ref riid,
out directoryShellItem) == NativeMethods.S_OK)
out NativeMethods.IShellItem directoryShellItem) == NativeMethods.S_OK)
{
frm.SetDefaultFolder(directoryShellItem);
}
}
if (this.Title != null)
{
frm.SetTitle(this.Title);
}

if (frm.Show(owner.Handle) == NativeMethods.S_OK)
{
NativeMethods.IShellItem shellItem;
if (frm.GetResult(out shellItem) == NativeMethods.S_OK)
if (frm.GetResult(out NativeMethods.IShellItem shellItem) == NativeMethods.S_OK)
{
IntPtr pszString;
if (shellItem.GetDisplayName(NativeMethods.SIGDN_FILESYSPATH,
out pszString) == NativeMethods.S_OK)
out IntPtr pszString) == NativeMethods.S_OK)
{
if (pszString != IntPtr.Zero)
{
Expand All @@ -104,7 +123,7 @@ private DialogResult ShowVistaDialog(IWin32Window owner)
private DialogResult ShowLegacyDialog(IWin32Window owner)
{
System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
if (folderBrowserDialog.ShowDialog(owner) == DialogResult.OK)
{
SelectedFolder = folderBrowserDialog.SelectedPath;
return DialogResult.OK;
Expand Down
36 changes: 35 additions & 1 deletion FolderBrowserEx/IFolderBrowserDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,44 @@ namespace FolderBrowserEx
public interface IFolderBrowserDialog

{
/// <summary>
/// Gets/sets the title of the dialog
/// </summary>
string Title { get; set; }

/// <summary>
/// Gets/sets folder in which dialog will be open.
/// </summary>
string InitialFolder { get; set; }

/// <summary>
/// Gets/sets directory in which dialog will be open
/// if there is no recent directory available.
/// </summary>
string DefaultFolder { get; set; }
string SelectedFolder { get; set; }

/// <summary>
/// Gets selected folder.
/// </summary>
string SelectedFolder { get; }

/// <summary>
/// Shows the folder browser dialog with a the default owner
/// </summary>
/// <returns>
/// System.Windows.Forms.DialogResult.OK if the user clicks OK in the dialog box;
/// otherwise, System.Windows.Forms.DialogResult.Cancel.
/// </returns>
DialogResult ShowDialog();

/// <summary>
/// Shows the folder browser dialog with a the specified owner
/// </summary>
/// <param name="owner">Any object that implements IWin32Window to own the folder browser dialog</param>
/// <returns>
/// System.Windows.Forms.DialogResult.OK if the user clicks OK in the dialog box;
/// otherwise, System.Windows.Forms.DialogResult.Cancel.
/// </returns>
DialogResult ShowDialog(IWin32Window owner);
void Dispose();
}
Expand Down
2 changes: 1 addition & 1 deletion FolderBrowserEx/WindowWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace FolderBrowserEx
{
class WindowWrapper : System.Windows.Forms.IWin32Window
{
private IntPtr _hwnd;
private readonly IntPtr _hwnd;

/// <summary>
/// Constructor
Expand Down
7 changes: 1 addition & 6 deletions Samples/MVVMBase/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ public class Command : ICommand

public Command(Action execute, Func<bool> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}

_execute = execute;
_execute = execute ?? throw new ArgumentNullException(nameof(execute));

if (canExecute != null)
{
Expand Down
7 changes: 1 addition & 6 deletions Samples/MVVMBase/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace MVVMBase
{
Expand Down
9 changes: 3 additions & 6 deletions Samples/NetFrameworkSample/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
using FolderBrowserEx;
using MVVMBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace NetFrameworkSample
{
public class MainWindowViewModel : ViewModelBase
{
private IFolderBrowserDialog _folderBrowserDialog;
private readonly IFolderBrowserDialog _folderBrowserDialog;
private string _result;

public MainWindowViewModel(IFolderBrowserDialog folderBrowserDialog)
Expand All @@ -35,6 +30,8 @@ private bool ShowFolderBrowserCommandCanExecute()

private void ShowFolderBrowserCommandExecute()
{
_folderBrowserDialog.Title = "Select a folder";
_folderBrowserDialog.InitialFolder = @"C:\";
if (_folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Result += $"{_folderBrowserDialog.SelectedFolder}\n";
Expand Down

0 comments on commit 9fae224

Please sign in to comment.