0

I would like to point new users to the proper directory to where the file type may be found. However, I don't want to hamper the efficiency of experienced users, as there are more specific sub folders that they may wish to remain in throughout multiple file openings.

I found some context for openfileDialog for c++ here: Initial directory is not working for CFileDialog

However I am interested in a VB solution. Here is Microsoft's documentation: https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.initialdirectory(v=vs.110).aspx

in which they state

The InitialDirectory property is typically set using one of the following sources: A path that was previously used in the program, perhaps retained from the last directory or file operation.

Here is my code:

If Not ImportDialog.InitialDirectory.Contains("Direct Access\Shell\Customer Invoices") Then
    ImportDialog.InitialDirectory = "....\Direct Access\Shell\Customer Invoices\"
End If

How can I determine if a user would automatically be sent to this directory or a subdirectory in the tree if getting InitialDirectory is unreliable?

djv
  • 15,168
  • 7
  • 48
  • 72
Brett
  • 3
  • 5
  • The `InitialDirectory` doesnt change when they pick a file from elsewhere. parse the filename selected and compare the path portion to the `InitialDirectory`. I typically have a user option variable like `LastOpenFolder` to store their selection even between app instances...(if I understand the post correctly). – Ňɏssa Pøngjǣrdenlarp Aug 30 '17 at 20:16
  • It is true that I could maintain a variable for the previous selected file name, however, this would still be less than ideal for a user that expected to be directed to their previous sub directory, even when in a new session. `InitialDirectory` is currently returning an empty string. Shouldn't it be returning the path that the file dialog will direct the user to? Is it only set after `ShowDialog` is called? – Brett Aug 30 '17 at 20:22
  • If you save the folder of their previous *path* choice and restore it as the InitialDirectory`, it would act like you describe. Dialogs are resources that should be disposed after use, so there is no chance for it to "remember" the last folder anyway (unless you are using the component). – Ňɏssa Pøngjǣrdenlarp Aug 30 '17 at 20:28
  • Ah, that clarifies it for me. So the documentation also states _A path read from a persistent source, such as an application setting, a Registry or a string resource in the application._ Does this mean I can access the most recent location to test whether or not I wish to set the `InitialDirectory` from the registry? – Brett Aug 30 '17 at 20:37
  • Ummm, that what I said in the first comment. If you save the *path portion* of the previous selection and always set that as the `InitialDirectory` before you open the Dialog, it will seem to remember. The `InitialDirectory` is the initial folder you want it to display, the dialog doesnt update it – Ňɏssa Pøngjǣrdenlarp Aug 30 '17 at 20:43
  • That's great and you have helped me understand how `InitialDirectory` works. I guess what I am now asking is what is the name of the registry key that holds "most recent path used in a fileDialog"? – Brett Aug 30 '17 at 21:01
  • There isnt one because there isnt just *one* FileXXXXDialog instance. You would not want your app to pick up the last folder used in excel would you? Their mention of the Registry is a place where you could store the `LastOpenFolder` between runs (but it is ill-advised). Save it to a variable for the life of the app, and save that to Settings when the app ends if you want it to seem to remember between runs – Ňɏssa Pøngjǣrdenlarp Aug 30 '17 at 21:06
  • Alright, I see that MS does not want us to have access to this information. I will go with this solution. Thank you for your help and patience! – Brett Aug 30 '17 at 21:12
  • No, thats not it - the `InitialDirectory` is not a secret - you set the value if you want to. But the user could have many apps running and opening File Dialogs. The path they selected from will be a part of the file name they click. Just use `IO.Path.GetDirectoryName(theFileTheySelected)` to find out the path they opened from. Its your data, so you save it for as long as you want. – Ňɏssa Pøngjǣrdenlarp Aug 30 '17 at 21:18
  • That is what I am interested though, out of all the different apps they are using file dialogs in, I want to know the most recent place they opened a file from (as this is the directory that automatically appears without me intervening, and yes I know now this has nothing to do with `InitialDirectory`). So I assumed it would be a registry value, but I think it may be hidden from other research ([here for example](http://www.vbforums.com/showthread.php?537988-RESOLVED-Get-Path-from-the-registry-key-in-VB-NET)). `IO.Path.GetDirectoryName(...)` appears to be the same as OpenFileDialog.FileName – Brett Aug 30 '17 at 21:28
  • Well, you can only fetch some other apps Registry settings if a) they save data to the Registry (no longer a good practice) and b) your know they do so and know where. `Path.GetDirectoryName()` is going to return just the path name *not* the file portion, so not the same. The point of that is to save where they last opened from so you can use that as the `InitialDirectory` next time. – Ňɏssa Pøngjǣrdenlarp Aug 30 '17 at 21:34
  • Right, I noticed the difference when implementing it. That's my fault about the registry stuff, I thought that the computer itself stored a registry value for system wide recently accessed files, I did not know it was unique to each app. Thanks! – Brett Aug 30 '17 at 21:53

1 Answers1

0

I saved the most recently opened folder and redirected the file path depending on if the user preferred a different path:

    If _lastOpenFolder = "" Then
        ImportDialog.InitialDirectory = "Direct Access\Shell\Customer Invoices\"
    Else
        ImportDialog.InitialDirectory = _lastOpenFolder
    End If
    If ImportDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
        _lastOpenFolder = ImportDialog.FileName.Substring(0, ImportDialog.FileName.LastIndexOf("\"))
        _lastOpenFolder = _lastOpenFolder.Substring(0, _lastOpenFolder.LastIndexOf("\"))
    End If
Brett
  • 3
  • 5