I am using Microsoft's CommonOpenFileDialog to allow users to select a Folder, but no files are visible when the dialog comes up. Is it possible to show files as well as folders when IsFolderPicker is set to true?
My current code looks like this
var dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
SelectedFolderPath = dialog.FileName;
}
Off the top of my head, this is how I did it
var dialog = new CommonOpenFileDialog
{
EnsurePathExists = true,
EnsureFileExists = false,
AllowNonFileSystemItems = false,
DefaultFileName = "Select Folder",
Title = "Select The Folder To Process"
};
dialog.SetOpenButtonText("Select Folder");
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
dirToProcess = Directory.Exists(dialog.FileName) ? dialog.FileName : Path.GetDirectoryName(dialog.FileName);
EDIT: Holy 2 years ago Batman!
Seems like few changes were made, snippet below seems to do the job
var openFolder = new CommonOpenFileDialog();
openFolder.AllowNonFileSystemItems = true;
openFolder.Multiselect = true;
openFolder.IsFolderPicker = true;
openFolder.Title = "Select folders with jpg files";
if (openFolder.ShowDialog() != CommonFileDialogResult.Ok)
{
MessageBox.Show("No Folder selected");
return;
}
// get all the directories in selected dirctory
var dirs = openFolder.FileNames.ToArray();
Not very sure if it even possible to do in a standard way, but even considering that yes, think about UI. Seeing contemporary folders and files in one place, but be able to select only folders, doesn't seem to me a good UI. IMHO it's better, and more "natural" way, to have one control populated with folders, and another (clearly readonly) populated with only files that have to be loaded.
Hope this helps.
If you want the user to select a folder only, have you considered using a FolderBrowserDialog?
Related
I am creating an installer and i would like to know how to use SaveFileDialog to get the file path where the user wants to install their mod to. The user should be able to click a button to open up the dialog, navigate to the folder and click select folder and once done is clicked have it stored as {DownloadPath}.
Try looking at the documentation
There is also a tutorial on this.
var filePath = string.Empty;
var saveFileDialog = new SaveFileDialog
{
Filter = #"Exe Files (.exe)|*.exe|All Files (*.*)|*.*"
};
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName != string.Empty)
{
filePath = saveFileDialog.FileName;
}
This is a WPF app with the latest version of the .NET framework and VS2015
on a Win 10 box.
I am trying to use the "CommonOpenFileDialog" from the Windows API code pack 1.1
to allow the user to establish a folder in which to do some stuff. The folder
can be either an existing folder, or a new one that the user specifies.
If the user wants to create a new folder, then I want them to be able to specify
the folder by editing the text within the "Folder:" textbox at the bottom of the
dialog. Within this context, the dialog would just be a means by which to
navigate to the folder in which the new one is to be created. My plan is to
validate the input within my code to check for a valid (existing) path, and
simply create the path if it does not exist.
Here is the code:
private void test1_folderSelectorDialog ()
{
if (CommonFileDialog.IsPlatformSupported)
{
var folderSelectorDialog = new CommonOpenFileDialog();
folderSelectorDialog.EnsureReadOnly = false;
folderSelectorDialog.IsFolderPicker = true;
folderSelectorDialog.Multiselect = false;
folderSelectorDialog.EnsureValidNames = false;
folderSelectorDialog.EnsurePathExists = false;
folderSelectorDialog.EnsureFileExists = false;
folderSelectorDialog.InitialDirectory
= "C:\\My_Initial_Directory";
folderSelectorDialog.Title = "test1_folderSelectorDialog";
if (folderSelectorDialog.ShowDialog() == CommonFileDialogResult.Ok)
TxBx_folder.Text = folderSelectorDialog.FileName;
this.Focus();
}
else
MessageBox.Show ("CommonFileDialog is not supported");
}
When I run the dialog and modify the text within the "Folder:" textbox,
then press "Select Folder", the dialog validates the input and issues a
dialog popup with the message:
"Path does not exist. Check the path and try again."
Please note that I have set "EnsureValidNames", "EnsurePathExists", and
"EnsureFileExists" to "false". (If they do not control dialog validation,
then what are they there for?)
I can right-click on the dialog window and use "new > Folder" to create a
new folder (which is what I'll have to do if I cannot resolve this issue),
but I'd rather do it the way that I am trying to do it, as it seems much
easier and more intuitive to do it that way.
How do I get the silly thing to shaddup and just accept the input without
passing judgement upon it?
Thanks!
If you want the user to select only the folder, then below code is enough
CommonOpenFileDialog dialog = new CommonOpenFileDialog()
dialog.IsFolderPicker = true
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
filesPath = dialog.FileName
}
I believe below things are not required
folderSelectorDialog.Multiselect = false
folderSelectorDialog.EnsureValidNames = false
folderSelectorDialog.EnsurePathExists = false
folderSelectorDialog.EnsureFileExists = false
When using a WinForm to click a button. and call save filDialog will come out
"C:\Windows\system32\config\systemprofile\Desktop".
http://i.stack.imgur.com/wH7J9.jpg
The challancing is this software must be runing in the "System" user.
But I using the SaveFileDialog for saving a file. will comes out the message like "C:\Windows\system32\config\systemprofile\Desktop".
I guess the problem is that when I using "system" user, the default user profile is no exist and counld no find the "Destop" floder path.
Because it will not have the "user profile" # "system" user.
This error comes out at I click the button, this application try to init the SaveFileDialog and also try genarate the icon shortcut on the left hand side so cause the error.
The dropdownbox also have the save problem.
http://i.stack.imgur.com/5Hy3S.jpg
Could any one know how to remove the icon shourt cut at the left hand side and the dropdown box icon also have the same problem.
using (var dialog = new SaveFileDialog())
{
dialog.DefaultExt = "txt";
dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
dialog.Title = "test";
dialog.AutoUpgradeEnabled = false;
dialog.InitialDirectory = Application.StartupPath;
try
{
DialogResult result = dialog.ShowDialog(this);
if (result == DialogResult.OK)
{
//do something
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
}
}
I just using very very very simple code.
ps. I am using C#.net 4.0 , vs2010 running at win2008r2 and win 7
Thanks all !!!
Try setting the InitialDirectory property of the SaveFileDialog
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = "c:\\yourDirectory";
You can also use this method to check if the my documents directory exists on disk.
Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
I'm using the CommonOpenFileDialog in the Windows API Code Pack as a folder picker dialog. I'm setting the InitialDirectory property to Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments). However, when I display the dialog, the path in the address bar is Libraries\Documents (not C:\users\craig\my documents as I'd expect). Additionally, if I just press the Select Folder button, I get a dialog saying that 'You've selected a library. Please choose a folder instead.'
Does someone know why my file path is being ignored, in favor of 'libraries\documents'? More importantly, how can I get the dialog to respect the InitialDirectory value I passed in?
The code I'm using for the dialog is:
if (CommonFileDialog.IsPlatformSupported)
{
var folderSelectorDialog = new CommonOpenFileDialog();
folderSelectorDialog.EnsureReadOnly = true;
folderSelectorDialog.IsFolderPicker = true;
folderSelectorDialog.AllowNonFileSystemItems = false;
folderSelectorDialog.Multiselect = false;
folderSelectorDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
folderSelectorDialog.Title = "Project Location";
if (folderSelectorDialog.ShowDialog() == CommonFileDialogResult.Ok)
{
ShellContainer shellContainer = null;
try
{
// Try to get a valid selected item
shellContainer = folderSelectorDialog.FileAsShellObject as ShellContainer;
}
catch
{
MessageBox.Show("Could not create a ShellObject from the selected item");
}
FilePath = shellContainer != null ? shellContainer.ParsingName : string.Empty;
}
}
Thanks,
-Craig
First of all, I'm sorry it took me so long to understand your question.
The message I see is when I try this is:
Cannot operate on
'Libraries\Documents' because it is
not part of the file system.
There's not much more to say. A library is a virtual folder that is an amalgamation of various different real folders.
There's no real way to avoid this error. You have asked the dialog to return a folder and the user has not selected a folder. The dialog therefore cannot fulfil its part of the deal.
If you descend further into the folder structure, into real folders, then the dialog will return you a real value.
Instead of
folderSelectorDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
try
folderSelectorDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
I managed to accidentally delete a backup of files I had which I then later recovered. The recovery has lost the files names and location and I am left with about 3000+ .indd (Adobeb InDesign) files.
My problem is I am trying to find the .indd file that I was working on with out having to open each one manually to check.
I know some of the words that I had and I am wondering if I could maybe read the .indd file using a binary reader looking for one of the keywords...I could build it in c# or whatever
Anyone got any ideas?
If regular search does not work, try the built in scripting, of which you can use Javascript, Visual Basic Script, or AppleScript to code. I'm going with JS...
I'm no expert, but I found this code snippet from page 101 of InDesignCS5_ScriptingGuide_JS.pdf and modified it a bit:
var folder = new Folder("C:/Path/To/Files");
var files = folder.getFiles('*.indd');
for (var i=0; i<files.length; i++) {
var file = files[i];
open(file):
var myDocument = app.activeDocument;
//Clear the find/change text preferences.
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
//Search the document for the string "Important Text".
app.findTextPreferences.findWhat = "Important Text";
//Set the find options.
app.findChangeTextOptions.caseSensitive = false;
app.findChangeTextOptions.includeFootnotes = true;
app.findChangeTextOptions.includeHiddenLayers = true;
app.findChangeTextOptions.includeLockedLayersForFind = true;
app.findChangeTextOptions.includeLockedStoriesForFind = true;
app.findChangeTextOptions.includeMasterPages = true;
app.findChangeTextOptions.wholeWord = false;
//Perform search
var myFoundItems = myDocument.findText();
if (myFoundItems.length) {
alert("FOUND!");
break;
}
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
myDocument.close();
}
Don't quote me on that, I did not actually run the code, but that's the idea.