Select both files and folders using OpenFileDialog - c#

I have researched a lot to find a suitable answer to this problem, but I am failing .
I can see multiple questions asked here and on other forums also , but no clear answer that brings a clear solution .
I want OpenFileDialog to select file/files for me as well as allow me to select folders also. eg. a. either Multiple files OR multiple folders (- most Prior)
b. combination of Files and Folders (-less Prior)
I figured our similar question here ( so please don't mark it as duplicate )
Question 1 [Answer links are broken]
Question 2 [Question isn't completely asking what my requirements are.]
Please guide me through some solution . I am a novice and a learner.
Any help or pointers would be very helpful .
Thanks.

You can't select folder with OpenFileDialog as well as you can't select files with FolderBrowserDialog. But there is an open source control for .net which allows you to select both files and folders you can check it here : OpenFileOrFolderDialog

var dialog = new OpenFileDialog();
dialog.ValidateNames = false;
dialog.CheckFileExists = false;
dialog.CheckPathExists = true;
dialog.FileName = "Must set default";
dialog.ShowDialog() // will allow both files and folders to be selected
Quite hack-ey.
Source

OpenFileDialog is used to open a file not folder
To allow selection of multiple files set Multiselect property to true.
For selecting Folder it's mentioned in the docs
If you want to give the user the ability to select a folder instead of a file, use FolderBrowserDialog.

You can create selctor "files or directories" and open standart OpenFileDialog or FolderBrowserDialog depending on user selection. Or you can create(or find) your custom file manager with options for selecting folders and files together.

Related

How to create resx files

I am trying to use Icons in a windows form application. I read that you can use resx files to do this. (I also read that resx files can be used for localization but this is not the point of this question)
I know more or less how to use a resx file if I have one(see below). What I don't know and I can't find anywhere is how to create these resx files (I know what these files are already)
Can someone teach me how to create a resx file that holds information on icons non-programmatically (the few responses about this in SO are programmatical ones)
The objective is to have a "resource.resx" file in my project that holds data about a "myicon.ico" file.
I am planning to use this as in
ResourceManager rm = new ResourceManager("resource",
typeof(Resource).Assembly);
Object ret= rm.GetObject("something");
if(ret!=null && ret is Icon)
return (Icon)ret;
else
return null;
Please don't point me to this link since I have already read it and could not find a practical way to do what I asked.
Assuming you're using Visual Studio:
Right-click your project
Select Add | New Item
Select Resources File
Give it a name (e.g. Resources)
Click Add
You will now have a Resources file in your project with the name you provided, and it should auto-open the resources editor. If it doesn't, double-click it in the project.
In the top-left of the editor, click the "Strings v" drop down and select "Icons".
Drag your icon into this screen.
Rename it to whatever you want (e.g. something).
If you named your resources file "Resources", you should be able to access the icon like so:
Object ret = Resources.ResourceManager.GetObject("something");
if (ret != null && ret is Icon)
return (Icon)ret;
else
return null;

C# - Microsoft.Win32.SaveFileDialog Filename Issue

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.

Where are paths previously used in the program stored (e.g. FileDialog paths)?

According to this msdn article one of the sources for InitialDirectory property used in FileDialog is:
A path that was previously used in the program, perhaps retained from the last directory or file operation.
...
So if you selected your first file from folder x, the next time you try to select a file it will open up the FileDialog with with folder x selected (saving you having to navigate there).
Playing around with notepad this seems to carry across opening a file, saving a file, opening a file and even when printing with "Microsoft XPS Document Writer" which brings up it's own dialog.
So my question is where is this value stored between dialogs? I would like to be able to see what it is and potentially change it? The specific area i would like to change it is in the "Microsoft XPS Document Writer" printer which brings up it's own dialog. So it's not as simple as just setting the initalDirectory Value.
It's stored in the registry, somewhere in HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\ (LastVisitedPidlMRU).
You should take a look at this link:
MRU locations are what you are looking at!
Here's a way for accessing it:
var openFileDialog1 = new OpenFileDialog();
string path = openFileDialog1.InitialDirectory;
// you can change path if you want
openFileDialog1.InitialDirectory = path;
// after you are donw you can display you dialog
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// do something
}
Another way is to use Directory.SetCurrentDirectory method which sets the application's current working directory
And from Microsoft website, it is stored at this location in the registry:
//The MRU lists for Windows Explorer-style dialog boxes are stored by file type for each user in the following registry key:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU
hope this helps

Is it possible to make a FolderBrowserDialog's default path show up in a library instead of the actual disk?

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

Customising the browse for folder dialog to show the path

What is the simplest way to customise the System.Windows.Forms.FolderBrowserDialog so a path can be entered using text in a textbox below the tree? This would make it easier to select unmapped UNC paths.
Looks like this KB has some supporting information.
Just this weekend I needed this. I looked and looked but could not find it. Resorted to writing it myself, based on that KB article, and some other things. Here ya go. FolderBrowserDialogEx (article in archive)
Full Source code. Free. MS-Public license.
Code to use it:
var dlg1 = new Ionic.Utils.FolderBrowserDialogEx();
dlg1.Description = "Select a folder to extract to:";
dlg1.ShowNewFolderButton = true;
dlg1.ShowEditBox = true;
//dlg1.NewStyle = false;
dlg1.SelectedPath = txtExtractDirectory.Text;
dlg1.ShowFullPathInEditBox = true;
dlg1.RootFolder = System.Environment.SpecialFolder.MyComputer;
// Show the FolderBrowserDialog.
DialogResult result = dlg1.ShowDialog();
if (result == DialogResult.OK)
{
txtExtractDirectory.Text = dlg1.SelectedPath;
}
Capabilities: shows editbox, shows full path in edit box. Can be used to browse printers or computers, as well as files+folders, or just folders.
Edit, 2018-05-31:
If the Codeplex link above does not work for you, this Git resource also exists.
Edit, 2022-02-11:
There is probably new repo of original author https://github.com/DinoChiesa/DotNetZip/blob/master/Zip/Resources/FolderBrowserDialogEx.cs
Try under code project folder browser - this allows customizing the dialog in many ways.
Also in social.msdn.microsoft.com there is a post that suggest creating a form of your own for that and even suggest the code for it.

Categories