C# OpenFileDialog Stored Paths - c#

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.

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\";

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

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

How launch batch photos/images in gallery view in C#

For a project I have folders of images. An example folder might be ImagesOfDogs, and the images inside would be sequentially named (1.png, 2.png, etc.). I have a button, and when I press this button I want to open all of the images in a folder at once, in one window. I could make my own window, but I would rather use the default Windows program so the user can edit the photos. This would look like the following (just imagine that black thing is a really cute dog pic):
Let's say the path to a folder is a string titled sPathToFolderOfDogs. On my button click, I want a method like this:
private void OpenCoolPicsOfDogs()
{
Process.Start(sPathToFolderOfDogs);
}
Except that will open all enclosed files in an image viewing application, preferably whichever is the user default. I have tried:
Process.Start("PictureViewer", sPathToFolderOfDogs);
...which opens PictureViewer and does not open my photos in PictureViewer,
Process.Start(sPathToParticularImage1);
Process.Start(sPathToParticularImage2);
etc etc
...which opens each in a new window,
and possibly most creatively/desperately:
String[] arrayOfAllMyPathsToParticularImagesInTheFolder;
(put all the strings in the array)
Process.Start(arrayOfAllMyPathsToParticularImagesInTheFolder);
Which just crashed. I know opening folders in applications through Process.Start should be possible because it is done in the docs here.* Is there a way to do it with images?
*relevant code from link:
// Start Internet Explorer. Defaults to the home page.
Process.Start("IExplore.exe");
// Display the contents of the favorites folder in the browser.
Process.Start(myFavoritesPath);
Thanks!
It turns out my question was a duplicate. However, I am not deleting it, as the powers that be suggest it is best to keep duplicates up as a "signpost" informing future searchers where the true path lies.
Here is the answer to my question (or more specifically, about half a dozen equally functional answers to my question).
TL;DR: The solution I am now using is from #bruceatk in the above link.

Choosing folders from list that I supply

I have a WPF application which maintains a set of virtual 'folders' (organizational concept), and I'd like to hook into something like the Folder Browse Dialog to select a folder (as a parent to some other kind of operation).
These are NOT denizens of the filesystem - what I need to do is be able to programmatically tell the dialog that at parent folder F, folders C1, C2, and C3 exist. I'd like the user to be able to browse and then select a folder - and then get the final selection information returned to me (what was the parent, which child was selected).
In other words, I want something that LOOKS like the Browse Folder Dialog - but I fill it with my own pretend set of folders and subfolders; and at the end I can get out the folder they actually chose.
Any way to do this in WPF?
Maybe you could restrict the folder opened AND populate with fake folders. Sounds unlikely to me though.
It will probably turn out more productive to create your own control with a treeview and your fake folders. And pop that up.
If you copy the layout of a file open dialog box as close as possible, most user will believe it is one and the usability will be decent.

Quick Question: How can I make sure the current directory is not changed because of OpenFileDialog?

say my application is installed in C:\programfiles\xyz\ I've some setting & other data files (*.dat files) in this directory too.
My application uses OpenFileDialog to open an image. Problem is that when ever user browses to some directory (say MyPictures) for an image. The current working directory of the application becomes that directory (MyPictures in this case).
After user inputs the image. I do some processing over it and save some values to imagedata.dat which will be located in the path where original application is installed.(C:\programfiles\xyz here )
In my code I'm just using FileStream("imagedata.dat",...,...) without any path prefix before the filename. Problem here is that application is crashing because it is searching for imagedata.dat in 'MyPictures\imagedata.dat'.
How can I avoid this?
You should be using absolute path names when saving data to files. The current working directory is controlled by the user, not by you (for example, if they launch your process from a shortcut then the working directory could've been changed before your process even starts up).
Also, you should never save anything under C:\Program Files during normal use. Doing this means your program needs to be running as an administrator, and unless you're doing administrator-y things then you should be able to run it as a regular user.
The correct thing to do in your case is to use the Environment.GetFolderPath() function to get the location of the ApplicationData folder and save your data under there. Just choose a sub-directory based on your application's name.
You could save it to GetCurrentDirectory then restore with SetCurrentDirectory. However, I agree wih codeka that using the appropriate GetFolderPath (probably ApplicationData, CommonApplicationData or LocalApplicationData) is a better solution.

Categories