Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VistaOpenFileDialog.InitialDirectory gets ignored #8

Open
soulflyman opened this issue Sep 26, 2019 · 11 comments
Open

VistaOpenFileDialog.InitialDirectory gets ignored #8

soulflyman opened this issue Sep 26, 2019 · 11 comments
Labels
good first issue Good for newcomers hacktoberfest https://hacktoberfest.digitalocean.com help wanted Extra attention is needed needs-design
Milestone

Comments

@soulflyman
Copy link

Initializing a dialog with the property InitialDirectory set to a local and existing folder is ignored.
The Dialog always shows the last folder where a file was opened.

 VistaOpenFileDialog dialog = new VistaOpenFileDialog();
 dialog.Filter = "JSON Dateien (*.json)|*.json";
 dialog.InitialDirectory = "C:\\";

Expected behaviour:
The VistaOpenFileDialog always schow the content of C:\ when InitialDirectory is set to "C:\".

Windows 10 Pro 1903 / .Net Framework 4.6

@augustoproiete augustoproiete added good first issue Good for newcomers help wanted Extra attention is needed labels Oct 4, 2019
@augustoproiete
Copy link
Member

augustoproiete commented Oct 4, 2019

Thanks for reporting @soulflyman. However, I believe this is the "correct" behavior as it matches what happens in all the other apps that come with Windows - i.e. They remember the last folder used.

You should be able to reproduce the same with Notepad or Paint.

@buildcomplete reported a similar issue with PR #2 so perhaps we need a way to override this behavior somehow (?), ideally as an opt-in setting that doesn't introduce a breaking change.

@soulflyman
Copy link
Author

That sounds great, I would be totaly fine with a new settign as you mentioned.

I was aware of the default behaviour (like notepad) but I exprected this behaviour to change when InitialDirectory is set manualy.
My assumption was that InitialDirectory meant to be the directory that is displayed every time the dialog is shown or at least in this running instance of the application. I was not expecting to be ignored even after closing and restarting the application.

@ssolmer
Copy link

ssolmer commented Feb 24, 2020

I am experiencing the same issue.
Experimented with different ways / order of setting the various VistaSaveFileDialog properties with no success. The Dialog always ignores the InitialDirectoy setting and uses the default.

I confirmed that '_initialDirectory' gets set to my desired path in VistaFileDialog.cs.
From there, the value makes its way to CreateItemFromParsingName(string path) in NativeMethods.cs, and then it disappears down the COM hole.

[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
public static extern int SHCreateItemFromParsingName([MarshalAs(UnmanagedType.LPWStr)] string pszPath, IntPtr pbc, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppv);

  public static Interop.IShellItem CreateItemFromParsingName(string path)
 {
     object item;
     Guid guid = new Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe"); // IID_IShellItem
     int hr = NativeMethods.SHCreateItemFromParsingName(path, IntPtr.Zero, ref guid, out item);
     if( hr != 0 )
         throw new System.ComponentModel.Win32Exception(hr);
     return (Interop.IShellItem)item;
 }

This SHCreateItemFromParsingName Method appears to fail, as the returned COM object seems to be null?

CreateItemFromParsingName

I added SetLastError = true to the method signature and tried Marshal.GetLastWin32Error() after the call, but it returns 0.

@augustoproiete augustoproiete added this to the Future milestone Nov 5, 2020
@nzain
Copy link

nzain commented Jan 29, 2021

While it is true that many windows applications have a "smart" default behavior to remember your last path, there are many situations where the developer of an app wants to specify a particular initial directory to help the user. For our application landscape this is even the default behavior, as our users expect to load from/save to specific project-related directories. Using the "smart" last directory from notepad/paint/word is annoying and useless here.

Plus: The Microsoft.Win32.OpenFileDialog has the exact same property and uses it as expected.

@gitjsdr26
Copy link

I've the same need.
Could you add a setting that could allow to set the Dialog box initial directory please ?
Thank you.

@gitjsdr26
Copy link

gitjsdr26 commented Apr 16, 2021

I went into Ookii library code. then I found how to take into account the InitialDirectory property.
You shall change this line of code into the file VistaFileDialog.cs
Into method internal virtual void SetDialogProperties(Ookii.Dialogs.Wpf.Interop.IFileDialog dialog)
change this piece of code

// Initial directory
if( !string.IsNullOrEmpty(_initialDirectory) )
{
	Ookii.Dialogs.Wpf.Interop.IShellItem item = NativeMethods.CreateItemFromParsingName(_initialDirectory);
	dialog.SetDefaultFolder(item);
}

to

// Initial directory
if( !string.IsNullOrEmpty(_initialDirectory) )
{
	Ookii.Dialogs.Wpf.Interop.IShellItem item = NativeMethods.CreateItemFromParsingName(_initialDirectory);
	dialog.SetFolder(item);
}

That's done.
Now the Ookii dialog will start into the InitialDirectory property.

Hope it could help.

@nzain
Copy link

nzain commented Apr 18, 2021

@gitjsdr26 you could create a PR for @augustoproiete

@gitjsdr26
Copy link

@nzain , sorry I don't know how to do this with GitHub.

@nzain
Copy link

nzain commented Apr 19, 2021

Ok, I've gathered some more information to shed light onto this issue.

We talk about an InitialDirectory property like the one documented for WPF's OpenFileDialog.InitialDirectory. The documentation says:

Gets or sets the initial directory that is displayed by a file dialog.

Users of Ookii Dialogs will expect the same behavior because the property has the same name. I agree, in many situations this property should not be set (notepad etc.), but that is a different story. If the property is set, it should work as expected.

The InitialDirectory is not to be confused with IFileDialog::SetDefaultFolder which is currently called. Here the documentation says:

Sets the folder used as a default if there is not a recently used folder value available.

I'm not sure if this (no recently used folder available) is ever the case. Setting the default folder never had any effect for me. If that functionality is required for anyone, it should be available as a DefaultDirectory property. It might be an obsolete artifact of old times - if anyone knows when this has an effect, please let me know.

PR?

The #2 PR is open for two years now (!) and suggests a similar thing. Although the author @buildcomplete mentions that it is working at least once in his scenario. His fix seems to be based on old code, but does the same thing as @gitjsdr26 suggests: change the call
IFileDialog::SetDefaultFolder(...) to IFileDialog::SetFolder(...).

I don't want to waste time with PR creation (I would change this single line, too).

@augustoproiete please let us know what you think about this :)

@nzain
Copy link

nzain commented Jun 20, 2021

I'm giving up on this repo. Fork it, if you want to see something happening. We won't be using it anymore.

@azaroth75
Copy link

For anyone falling in.
https://stackoverflow.com/questions/36264956/vistaopenfiledialog-does-not-start-with-the-correct-initial-directory
Xerillio wrote :
// To show the contents of "C:\", i.e. the parent of "Users":
dialog.FileName = @"C:\Users"
// To show the contents of the "Users" directory:
dialog.FileName = @"C:\Users\"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers hacktoberfest https://hacktoberfest.digitalocean.com help wanted Extra attention is needed needs-design
Development

No branches or pull requests

6 participants