I was just at Open directory dialog, and they said "get this package, and do this and this to get a folder select window to show up". Well, that all works great, using the Windows API Code Pack-Shell package. However, now I want to get the actual folder that is selected. I didn't notice them mentioning this anywhere.
I tried to do string folderLocation = Convert.ToString(dialog); (dialog is the variable for opening the folder window), but that only gave me like the property of the variable. I also tried this: CommonFileDialogResult result = dialog.ShowDialog();
string folderLocation = Convert.ToString(result);
But that just gave me "Ok" - which I take it is the result of it, instead of the actual folder.
The result of ShowDialog just indicates wether the user clicked OK, cancel, or just closed the window.
CommonOpenFileDialog can be used for both files and folders, so it's a bit surprising when used as a folder picker, but the path is stored in FileName.
var dlg = new CommonOpenFileDialog();
dlg.IsFolderPicker = true;
if(dlg.ShowDialog() == CommonFileDialogResult.Ok) {
Console.WriteLine(dlg.FileName);
}
If I understood correctly, you want to get Folder for a selected file? If that's the case, you can take FileInfo for that file, and extract folfer from it. Like this:
System.IO.FileInfo fInfo = new System.IO.FileInfo(oFD1.FileName);
MessageBox.Show(fInfo.DirectoryName);
PS. oFD1 is OpenFileDialog
Related
In my application I use both OpenFileDialog and FolderBrowserDialog on button click handlers:
var fileDialog = new System.Windows.Forms.OpenFileDialog();
var folderDialog = new System.Windows.Forms.FolderBrowserDialog();
Strange thing is that when calling OpenFileDialog it starts in explorer from folder in which file was chosen last time.
But FolderBrowserDialog opens MyComputer each time in explorer no matter what folder was chosen last time. How can I get same behavior (remembering last chosen folder) for `FolderBrowserDialog'?
It's also interesting where 'OpenFileDialog' stores folder of last chosen file? Does windows stores it for each application?
You can set FolderBrowserDialog's selected folder using the SelectedPath property before opening:
var folderDialog = new System.Windows.Forms.FolderBrowserDialog();
folderDialog.RootFolder = System.Environment.SpecialFolder.MyComputer;
folderDialog.SelectedPath = <variable_where_you_stored_the_last_path>;
For example:
private string _lastFolderDialog = null;
// ...
var folderDialog = new System.Windows.Forms.FolderBrowserDialog();
folderDialog.SelectedPath = _lastFolderDialog;
if(folderDialog.ShowDialog() == DialogResult.OK)
{
_lastFolderDialog = folderDialog.SelectedPath;
}
As for the OpenFileDialog, I think you mean:
fileDialog.InitialDirectory =
Environment.GetFolderPath(System.Environment.SpecialFolder.MyComputer);
However that won't work, since MyComputer doesn't have a path. Try this instead:
fileDialog.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
You can check for other CLSIDs in the registry under HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CLSID
As you have already discovered, if InitialDirectory is set to null, it'll remember the last opened folder. This won't happen with FolderBrowserDialog though
All that said, and as I stated in the comments, the FolderBrowserDialog is pretty much obsolete and you should not use it at all. According to the MSDN for the native API (SHBrowseForFolder) that supports it:
For Windows Vista or later, it is recommended that you use IFileDialog with the FOS_PICKFOLDERS option rather than the SHBrowseForFolder function. This uses the Open Files dialog in pick folders mode and is the preferred implementation.
You may want to check this question (which in turn links to this page) or this other question on how to implement IFileDialog with FOS_PICKFOLDERS in .NET
I have a problem where if you set the filename in the dialog box to a sub directory within the initial directory you set it to and then clicking 'Save', the dialog window doesn't actually save the file but opens the sub directory which I could still interact with.
For example If I set the initial directory for the dialog to 'C:\MainDir' and that directory consists of SubDir1, SubDir2, then in the save file dialog I could see that I am in the initial directory with two sub directories. If I set the filename to SubDir1 (no extension) in the dialog, and then I hit 'Save', what happens is instead of saving the file as 'filename.extension' the dialog opens the directory specified by the file name.
Here's what I currently have:
SaveFileDialog dlg = new SaveFileDialog();
dlg.DefaultExt = ext;
dlg.AddExtension = true;
dlg.FileName = filename;
dlg.Filter = filter;
dlg.FileOk += OnFileDialogOk;
dlg.InitialDirectory = dir;
bool? dlgRes = dlg.ShowDialog();
Is this something that can be easily fixed?
Quick Answer: No.
You cannot override the default save method of Windows OS.
What you can do is perhaps to verify whether the filename you wanted to use (in this instance, SubDir) exists already as a directory. If it does, then you would need to change that name, as that will only manifest the behavior you've already seen.
Side Note: Just imagine you have a very important folder which contains critical files, and Windows would let you save a file that is named with that directory. That is a disaster waiting to happen.
The only ways I can think of doing this are a bit extreme:
You could roll your own dialog
You could modify the functionality of the standard dialog
The answers found here: Customizing OpenFileDialog could help with that.
I guess I should also note that while it may seem helpful to accommodate this kind of input and automatically append the extension, it'll be counter-intuitive to many users who will expect the default behaviour.
In short, I'd probably think twice about this.
I'm using OpenFileDialog (.Net Framework 4, Windows 10) and I've noticed that it will allow the user to specify a URL as the file name (e.g., http://somewebsite/picture.jpg). This is very useful for my application, so I don't intend to disable it. The way it works is downloading the file into the user's temp directory and returning the temporary file name in the dialog's Filename property. This is nice, except for the fact that the user starts to build up garbage in his/her temp directory.
I would like to tell when a file was downloaded by the OpenFileDialog class (as opposed to a previously existing file), so I can clean up by deleting the file after use. I could check if the file's directory is the temp directory, but that's not very good since the user might have downloaded the file him/herself.
I've tried intercepting the FileOK event and inspect the Filename property to see if it is an HTTP/FTP URI, but despite what the documentation says ("Occurs when the user selects a file name by either clicking the Open button of the OpenFileDialog") it is fired after the file is downloaded, so I don't get access to the URL: the Filename property already has the temporary file name.
EDIT: This is an example of what I'like to do:
Dim dlgOpenFile As New System.Windows.Forms.OpenFileDialog
If dlgOpenFile.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then Return
''//do some stuff with dlgOpenFile.Filename
If dlgOpenFile.WasAWebResource Then
Dim finfo = New IO.FileInfo(dlgOpenFile.Filename)
finfo.Delete()
End If
In this example, I've imagined a property to dlgOpenFile "WasAWebResource" that would tell me if the file was downloaded or originally local. If it's the first case, I'll delete it.
There's no obvious way to do this, but as a workaround, how about checking where the file lives? It looks like by default this dialog downloads files to the users Temporary Internet Files directory, so you could introduce some code that looks something like this:
FileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
string temporaryInternetFilesDir = Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache);
if (!string.IsNullOrEmpty(temporaryInternetFilesDir) &&
dialog.FileName.StartsWith(temporaryInternetFilesDir, StringComparison.InvariantCultureIgnoreCase))
{
// the file is in the Temporary Internet Files directory, very good chance it has been downloaded...
}
}
I know that if I set SelectedPath before I show the dialog I can get it to have a folder open by default when the dialog opens. However, the folder I want to use is very far down the list alphabetically. I have that same folder as one of my Libraries in Windows and it shows up at the of the listing, is there any way to have it default to the library version of the folder instead of the hard drive version of the folder?
Another potential solution would be if it did still use the drive version but it automatically scrolled the window down to where it was selected. Is there any way to do either of these solutions?
How it currently shows up
How I would like it to show up
Set your root folder and selected path as such and it will auto-scroll there for you on the dialog opening:
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.RootFolder = Environment.SpecialFolder.MyComputer;
dlg.SelectedPath = #"E:\Vetcentric";
dlg.ShowDialog();
The problem you run into is that if you watch the property assignments after selecting a folder located in the libraries hierarchy, it will still assign it to the genereic path that you would get via going through my computer.
Use a Reset() call. This will make it auto-scroll.
string prevpath = folderBrowserDialog1.SelectedPath;
folderBrowserDialog1.Reset();
folderBrowserDialog1.SelectedPath = bc.myWorkingDir;
folderBrowserDialog1.ShowNewFolderButton = true;
DialogResult dr = folderBrowserDialog1.ShowDialog();
if (dr == DialogResult.OK || dr == DialogResult.Yes)
{
bc.myWorkingDir = folderBrowserDialog1.SelectedPath;
}
folderBrowserDialog1.SelectedPath = prevpath;
Just set the path Libraries\VetCentric...
before you open should do it I think.
The easiest way would probably be to put shortcuts to the folders you want into your starting folder. Then, just double click on the shortcut and it will take you to your folder.
Otherwise you will need to use the Shell Library API
See: Using Libraries in your Program
I'm opening a SaveFileDialog with an initial directory based on a user-defined path. I want make sure this path is valid before passing it in and opening the dialog. Right now I've got this:
Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
if (!string.IsNullOrEmpty(initialDirectory) && Directory.Exists(initialDirectory))
{
dialog.InitialDirectory = initialDirectory;
}
bool? result = dialog.ShowDialog();
However, it seems \ is slipping by and causing a crash when I call ShowDialog. Are there other values that could cause crashes? What rules does the InitialDirectory property need to follow?
The quick and easy way to fix it would be to get the full path:
dialog.InitialDirectory = Path.GetFullPath(initialDirectory);
This will expand relative paths to the absolute ones that the SaveFileDialog expects. This will expand just about anything that resembles a path into a full, rooted path. This includes things like "/" (turns into the root of whatever drive the current folder is set to) and "" (turns into the current folder).