Limit folderbrowserdialog to one folder - c#

I've got a folderbrowserdialog that will open to a certain folder, lets say C:\Temp.
Within this folder are a number of folders, e.g C:\Temp\1\ C:\Temp\2\ etc.
Now i only want the user to be able to select a folder inside C:\Temp, e.g select 1 or 2.
Is there an easy way to do this within the folderbrowserdialog or shall i rethink my approach and just display the folders in a dropbox or something and have them select if from there?
Cheers

If it's not a SpecialFolder you can't limit it to this folder only. If it is a special folder, then you can set a RootFolder property for FolderBrowserDialog.
var d= new System.Windows.Forms.FolderBrowserDialog { RootFolder= Environment.SpecialFolder.LocalApplicationData};
d.ShowDialog();

Related

How do I, in a compiled C# class, read a file using System.IO from another directory outside my main directory without typing the whole path?

If my directories look like this:
MyProject
MyProject
some_dir
file_I_wanna_read
here
Here.cs
How do I read file_I_wanna_read from Here.cs, without having to type the whole path?
(eg. "C:\my_user\Projects\MyProject\MyProject\some_dir\file_I_wanna_read")
I've already tried writing the path as if I were at the solution:
MyProject\some_dir\file_I_wanna_read
And also as if I were in the .csproj:
some_dir\file_I_wanna_read
But those don't work.
Here's a solution.
It relies on getting the FileYouWantToRead copied into the EXE's location where it's findable.
Create a simple, out-of-the-box WinForms app.
Right-click the project, select Add then New Folder, name the folder "SomeDir"
Right-click the new "SomeDir" folder and select Add and then New Item
In the dialog that pops up, search for "Text", pick Text File and name the file "FileIWantToRead.txt"
When the file opens, type "This is the file I want to read" and save the file
Right-click the new "FileIWantToRead.txt" file and choose properties
In the properties pane, set Build Action to Content and Copy to Output Directory to Copy Always
Go back to the form designer, drop a button and a label control on the form
Double-click the button
In the "Form1.cs" button click handler that opens, put this code:
Form1.cs button click handler:
private void button1_Click(object sender, EventArgs e)
{
var fullExeName = Assembly.GetExecutingAssembly().CodeBase;
if (fullExeName.StartsWith("file://"))
{
fullExeName = fullExeName.Substring(8);
}
var exeDir = Path.GetDirectoryName(fullExeName);
var fileName = Path.Combine(exeDir, "SomeDir", "FileIWantToRead.txt");
var content = File.ReadAllLines(fileName)?.First();
label1.Text = content ?? "Not Found";
}
The "Copy Always" action will cause your text file to get copied to the output EXE's folder. In this case, since it's within the "SomeDir" folder, it will get copied into a "SomeDir" folder under the output folder.
I get the EXE's folder from the executing assembly (i.e., the running exe) and use the various utilities in System.IO to get the right folder and file names. After clicking the button, the first line of that file will show in the label.
First you can get current directory of your program. (Read these official docs for further help)
Then you can access your file with directory related to current directory.
Your string of directory accessing should be something like this:
..\some_dir\file_I_wanna_read
(Double dots mean go one directory backward or up.)

How to get temporary filename when use OpenFileDialog in C# Winform

In C#-WinForms I use a OpenFileDialog. I want to get all files/filenames in a selected folder, without pressing the ok button of the OpenFileDialog.
Is there a good way to achieve this?
if i understood your question in the right way, you want to get the Filenames out of your selected folder. To archive that, you could use a FolderBrowserDialog.
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
if(folderBrowser.ShowDialog() == DialogResult.OK)
{
var path = folderBrowser.SelectedPath;
System.Windows.IO.DirectoryInfo info = new System.Windows.IO.DirectoryInfo(path);
foreach(var file in info.GetFiles())
{
//do what you want with your files.
//example
listbox1.Items.Add(file.Name);
}
}
If you got some further question, feel free to ask.
-- EDIT --
Ok now i understood what you mean.
You want to get the files out of a folder when clicking on it.
Maybe build your own dialog (simple new window). With a treeview on it. And if you click on a treeviewitem, on ther other side it displays you the files and subfolder.

WPF C# Opening files in multiple directories

I have 3 open file buttons which open a file open dialog, whenever one file is opened the starting directory for the next button is always the same as the last button that was used.
I need to be able to have each button open only the last directory that it was associated with, not what the last button opened was associated with.
How can make each dialog open in the directory that that specific dialog was opened in last?
Example, I have 3 buttons i want to open in the following order:
Btn1 Open File in dir C:\temp\1 then
Btn2 Open File in dir C:\temp\1 then change to C:\temp\2
Btn3 Open File in dir C:\temp\2 then change to C:\temp\3
Btn1 Open File in dir C:\temp\1 NOT in C:\temp\3
declare some private fields in your class:
string startLocationForDialog1 = "C:\";
string startLocationForDialog2 = "C:\";
string startLocationForDialog3 = "C:\";
Then in your methods, when you create the open file dialog, set the starting location to the value of the corresponding variable.
After the file is selected, save the location of the file (without the file name) in the corresponding variable. Next time you press the same button, you use that variable which contains the last location from which a file was selected.

Form Regions for Outlook and VSTO remove WebViewPane

I have just purchased: http://www.add-in-express.com/outlook-regions/
The reason why I have purchased that product is because I need to show a custom form on the main view of outlook when a specific folder is selected. I have managed to do so by doing the following:
Create a new Project int visual studio of type (Outlook 2010 Add-in)
Once that project is created I add:
Because I want that form to show on the main folder view of mail (replace all other views) I select this option:
I click next and follow all the defaults in order to create the form.
Once that form is created I add the buttons and images I need. In this example I will just add a button.
when I then run outlook and select my inbox folder this is what shows up:
(In other words every time I select a folder of type olMailItem that form shows up)
So now I solve my goal to display my custom form on the "main view of outlook"
Now my question is how can I show that form only on specific folders. For example I do not want to show that form when the folder "Inbox" is selected but I do want to show it when the folder "Outbox" is selected for example
Let's try to hide the form and show the default view when the button is clicked.
In order to solve that problem I have looked at: http://www.add-in-express.com/forum/read.php?FID=5&TID=4540
I have done the same steps but the form shows up again! In other words the code behind of the button looks like:
private void button1_Click(object sender, EventArgs e)
{
// get current folder in this case its inbox the one that is selected
MAPIFolder currentFolder = Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder;
Globals.ThisAddIn.ADXOlForm1Item.FolderName = string.Empty;
// clear web properties DO NOT SHOW WEB VIEW
currentFolder.WebViewURL = string.Empty;
currentFolder.WebViewOn = false;
// RESET FOLDER BY SELECTING A DIFFERENT ONE THEN THE SAME ONE
NameSpace nameSpace = Globals.ThisAddIn.Application.GetNamespace("MAPI");
MAPIFolder outboxFolder = nameSpace.GetDefaultFolder(OlDefaultFolders.olFolderOutbox);
Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder = outboxFolder; // CHANGE FOLDER TO A DIFFERNT ONE
System.Windows.Forms.Application.DoEvents();
Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder = currentFolder; // SET INBOX AGAIN
}
when I run that code the default view shows up for 1 second then it get's replaced with the form!
-------------------------------------------Edit-------------------------------------------
When I add the form region Addin Express adds a ADXOlFormsManager and a ADXOlFormsCollectionItem for the form that I created. Based on your answer I have done:
#region ADXOlForm1
// TODO: Use the ADXOlForm1Item properties to configure the region's location, appearance and behavior.
// See the "The UI Mechanics" chapter of the Add-in Express Developer's Guide for more information.
ADXOlForm1Item = new ADXOlFormsCollectionItem();
ADXOlForm1Item.FolderName = "MyCustomFolder"; // <---- ADDED THIS LINE HOPING TO SHOW THIS FORM ONLY WHEN THAT FOLDER IS SELECTED
ADXOlForm1Item.ExplorerLayout = ADXOlExplorerLayout.WebViewPane;
ADXOlForm1Item.ExplorerItemTypes = ADXOlExplorerItemTypes.olMailItem;
ADXOlForm1Item.UseOfficeThemeForBackground = true;
ADXOlForm1Item.FormClassName = typeof(ADXOlForm1).FullName;
this.FormsManager.Items.Add(ADXOlForm1Item);
#endregion
I was hoping for that form to only show up in MyCustomFolder but it does shows up when selecting any folder of type ADXOlExplorerItemTypes.olMailItem. Perhaps I am doing something wrong....
In other words I was hoping for only the MAPIFolder MyCustomFolder folder had the properties
WebViewOn=true;
WebViewURL = "...AppData\Local\Temp\AddinExpress\ADXOlFormGeneral.html"
but as I traverse all the folders in outlook I can see that all of them have those properties even after specifying ADXOlForm1Item.FolderName = "MyCustomFolder";
Thank you for choosing Add-in Express Regions.
I understand the "main view of outlook" as the folder that Outlook shows when it is started. By default, that folder is the top-level folder of the message store. Note that this can be changed, see File | Options | Advanced | Start Outlook in this folder. I use this setting so that my Outlook shows me the Inbox at start-up.
The below is a citation from the section Context-Sensitivity of Your Outlook Form, see see the PDF file in the folder {Add-in Express}\Docs\ on your development PC:
ADXOlFormsCollectionItem provides a number of properties that allow specifying the context settings for your form. Say, you can specify item types for which your form will be shown. Note that in case of explorer, the item types that you specify are compared with the default item type of the current folder. In addition, you can specify the names of the folders for which your form will be shown in the FolderName and FolderNames properties; these properties also work for Inspector windows – in this case, the parent folder of the Outlook item is checked. An example of the folder path is "\Personal Folders\Inbox".
A special value in FolderName is an asterisk ('*'), which means "all folders". You can also specify message class(es) for which your form will be shown. Note that all context-sensitivity properties of an ADXOlFormsCollectionItem are processed using the OR Boolean operation. That is, specifying e.g. folder names extends, but not limits, the list of contexts for which your form will be shown.
That is, if you need to show the form for a given folder, specify the path to that folder in the FolderName/*FolderNames* property.
Regards from Belarus (GMT+3),
Andrei Smolin, Add-in Express Team Leader

Visual C# 2008 Control to set a path

Greetings,
What control in Visual C# 2008 would allow me to set a path and get the value of that path.
For example:
I want the user to click a button then select a path where he/she would do the operation such as save a file in selected path.
You want to use a FolderBrowserDiaglog
var folderBrowserDiaglog = new FolderBrowserDiaglog();
if ( folderBrowserDiaglog.ShowDialog() == DialogResult.OK )
{
string path = folderBrowserDiaglog.SelectedPath;
}
I don't mean to repeat, but none of the other answers seemed to be exactly what you wanted.
There are 3 controls: FolderBrowserDialog, OpenFileDialog, and SaveFileDialog. There names are pretty intuitive. You use all 3 the same way. Tanascius has a good example in his post. The folder dialog you would use if you want the user to select a whole folder to look at. The open you would use if you want the user to select one or many files to open. Save is the same as open, but you would use it when saving a file out.
Hope this helps.
The FolderBrowse Dialog....
SaveFileDialog (for a file) or FolderBrowserDialog (for a folder). Located in the dialogs tab in the toolbox.
Assuming Windows Forms, try the OpenFileDialog. All common dialogs descend from CommonDialog, so looking at the descendents of that class may help, too.

Categories