How to make the OpenFileDialog remember it has been in %temp%? - c#

I'm making a log viewer application in C#.NET/Forms on Win8 desktop.
I have a problem when using the System.Windows.Forms.OpenFileDialog. If I select a file from the %temp% directory and click OK, and then want to select another file, the OpenFileDialog refuse to remember that I last visited the %temp% directory. It always reverts to displaying the last non-%temp% directory I visited, which is very annoying since my app typically opens various log files from %temp%.
Precondition:
- OpenFileDialog created and existing.
- OpenFileDialog has InitialDirectory = "C:\"
Scenario:
- ShowDialog(): Displays C:\ - OK.
- Change directory to C:\logfiles\test.txt and click OK to close.
- ShowDialog(): Displays C:\logfiles\ - OK.
- Change directory to %temp% (which expands to C:\Users\joe\AppData\Local\Temp) and click OK to close.
- ShowDialog(): Displays C:\logfiles\ - FAIL! Here it should show C:\Users\joe\AppData\Local\Temp but it doesn't. It reverts to the last directory I selected that is not in %temp%. Why?
Question: How to prevent this behavior?

You can use InitialDirectory property documented: http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.initialdirectory.aspx
Example:
fdlg.InitialDirectory = Path.GetTempPath();
Please check the below link.
OpenFileDialog.RestoreDirectory fails for %temp% location? Bug or Feature

Related

How do I know which folder Save As… will start in?

I want to avoid opening the Save As dialog in one specific folder. If I open a file from that folder then do a Save As…, it starts in that same folder (as expected). I thought I could just examine InitialDirectory after calling new SaveFileDialog() and change it if necessary, but it is an empty string.
Directory.GetCurrentDirectory() returns the folder containing the executable.
var dialog = new SaveFileDialog();
Console.WriteLine(Directory.GetCurrentDirectory()); // Prints "Z:\Documents\Projects\ProjectName\bin\x64\Debug"
Console.WriteLine(dialog.InitialDirectory); // Prints empty string
How do I ask Windows (7 or 10) which folder the Save As… dialog will start in?
Edit
This is a completely different question than "How do I set the initial directory?". I want to know what the initial directory is going to be before the dialog opens so that I can change it only in the case where it is going to be one specific directory.
I assume that you want the savefiledialog to start from the folder you want
Try this to change the initial directory to whatever you want
dialog.InitialDirectory="C:\Users\Your_Name\Desktop\";

FolderBrowserDialog on company network to select subfolder

In my WPF app the user needs to select a folder, which path is in the company network. I use the System.Windows.Forms.FolderBrowserDialog and the following code gets executed on a button click event:
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.SelectedPath = "\\\\company.net\\data\\_Confidential";
DialogResult result = fbd.ShowDialog();
When the FolderBrowserDialog opens, the system automatically scans for other network devices and that causes the following problem:
The network tree gets filled with other devices and causes my SelectedPath to scroll away. This is pretty annoying when a user starts searching for a special subfolder, because he has to scroll down or his selection clicks can hit a newly added device (lost focus).
How can i avoid this problem?
Thoughts:
Can I extend/overwrite the System.Environment.SpecialFolder Enum and
set fbd.RootFolder = System.Environment.SpecialFolder.MySepcialNetworkPath;
Should I access the network folder with another dialog/control?
Should I remove the "Browse..." Button in my View and instead scan the whole \\\\company.net\\data\\_Confidential path and provide a combobox/other selection control(e.g. own subfolder-tree)?
The FolderBrowserDialog is 'adopting' your PC settings depending on how your Network Discovery is configured on your PC/network. By doing so your folder browsing experience will be consistent over other applications.
Although what you see is default behavior of the FolderBrowserDialog, you may also look at this: https://stackoverflow.com/a/15440926/5793786 Solved an issue somewhat similar to yours #Frank
While I was searching for the same problem I came across this thread:
How to use OpenFileDialog to select a folder?
Where the user uses a "CommonOpenFileDialog" available in the Nuget Package "WindowsAPICodePack-Shell".
This solved my issue, though it uses the OpenFileDialog interface.
Then the network drive can just be browsed.

How to retrieve the openfiledialog opening path? C#

I have a openfiledialog in my application. I do not set the initial directory and user can navigate to open file and import it to the application (for example imports excel file from C:\ Test location). Next time, when user wants to open another file, system (by default, since initial directory is not set) remember the previous location and openfiledialog opens in that directory (C:\ Test). Is there anyway to retrieve the path that openfiledialog opens? I am sorry if I couldn't explain my problem well and thanks for the help.
Registry is hard to handle, I would suggest that you use Application Properties instead.

Bizarre FolderBrowserDialog behaviour

I'm supporting an old version of a C# application, running on .NET 3.5. We've found an issue with the FolderBrowserDialog on Windows Vista (either 32 or 64-bit).
Basically what happened is that the dialog would appear, but only the root Desktop node would be shown, not even able to expand it to show anything else. Obviously, that's impossible to use.
After a huge amount of trial and error I eventually managed to get something useable, by setting the RootFolder property before the rest of the setup:
FolderBrowserDialog browsePath = new FolderBrowserDialog();
browsePath.RootFolder = Environment.SpecialFolder.MyComputer;
browsePath.SelectedPath = this.textBoxTo.Text;
browsePath.Description = TextResources.OutputTargetCaption;
browsePath.ShowNewFolderButton = true;
if(browsePath.ShowDialog(this) == DialogResult.OK)
{
this.textBoxTo.Text = UpdateLocation(browsePath.SelectedPath);
}
This almost works; however, I've got the bizarre issue that then the SelectedPath (by definition the contents of textBoxTo) is a path to within the current user's home directory, it won't automatically browse to that path, instead just showing the My Computer node expanded to one level. It's perfectly fine for any other path.
I'm sure your first guess would be a permissions issue, as was my intuition. It doesn't appear to be, this issue occurs running normally and as an Administrator, for both standard and Administrator accounts. It's a clean install, of course, no weird permissions or anything.
This is pretty annoying when all of our defaults are within the current user's directory!
Note: This only happens within the application; it's not reproducible with a small test application, as far as I've seen.
Any ideas on what could be causing this?
Update: Screenies:
This is the behaviour I want (obtainted from a little test app)
This is what I get with the default property
This is what I get by setting the root to My Computer
Note: The last image had the same SelectedPath set as the expected image...
I had a similar problem. In Windows Vista and Windows 7 the following code:
browsePath.RootFolder = Environment.SpecialFolder.MyComputer;
returns the Desktop. If you look in Windows Explorer, the root of the tree is Desktop, not My Computer like it was in Windows XP.
To solve this problem use this instead:
browsePath.RootFolder = #"C:\";
Every Windows computer has a C:\ drive, so this will solve your problem.
I hope this helps you.
If you're only accessing the users private folders, use
browsePath.RootFolder = Environment.SpecialFolder.Personal
Only the specified folder and any subfolders that are beneath it will
appear in the dialog box and be selectable. The SelectedPath property,
along with RootFolder, determines what the selected folder will be
when the dialog box is displayed, as long as SelectedPath is an
absolute path that is a subfolder of RootFolder (or more accurately,
points to a subfolder of the shell namespace represented by
RootFolder).
In short, you can not input someones private folder as a startup selectedPath unless RootFolder is inside the current users private folder already.
For details, see: http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.rootfolder.aspx
VB.NET code
Dim fdb As New FolderBrowserDialog
With fdb
'.RootFolder = Environment.SpecialFolder.MyComputer
'this folder don't exists in vista, the my computer folder was renamed to computer (in spanish "mi pc" to "equipo")
'try with another initial folder
.RootFolder = Environment.SpecialFolder.Desktop
'You can set the desktop as home directory because users typically already have shortcuts or the left side menu to navigate
Dim dr As DialogResult = .ShowDialog
If _
dr = DialogResult.OK Or _
dr = DialogResult.Yes Then _
If IO.Directory.Exists(.SelectedPath) = True Then _
Me.textBoxTo.Text = UpdateLocation(.SelectedPath)
End With
basically, try another directory and make sure the chosen directory exists.
if you're still having problems, is probably due to some fault in the system.

C# OpenFileDialog Stored Paths

In my application I have two places where the user needs to select a file. In both cases, the files are in different directories, but they are generally the same between runs.
The OpenFileDialog appears to be defaulting to the last directory used, but this is useless to me since it is almost always the wrong folder, and I end up alternating between the two folders. What I would like is to somehow have the first dialog remember the path that was used the last time it was opened, and the second to remember its own path as well.
Example: Path A is C:\foo\bar\something\x.dll, Path B is C:\foo\baz\whatever\y.xml
Dialog a opens and I select A, then later dialog b opens(defaulted to A) and I have to navigate back and up to B.
When I open the app again dialog a opens (defaulted to B) and I have to navigate back up to A again.
I would like to avoid all of this extra navigation by remembering the paths separately. Is there a good way to do that?
When you open each dialog, just set the dialog's InitialDirectory property to the folder that was last used for that dialog.
Granted, this will require saving the directory for each dialog, but it will provide the behavior you are looking to achieve.

Categories