i'm using 'folderBrowserDialog1.SelectedPath' to have a path to chosen folder. How can i see the names of another folders in folder which is selected?
DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
foreach (DirectoryInfo subDir in dir.GetDirectories())
{
Console.Out.WriteLine(subDir.Name);
}
You're looking for the Directory.GetDirectories method.
The FolderBrowserDialog component is displayed at run time using the ShowDialog method. Set the RootFolder property to determine the top-most folder and any subfolders that will appear within the tree view of the dialog box. Once the dialog box has been shown, you can use the SelectedPath property to get the path of the folder that was selected
You probably want to use the DirectoryInfo.GetDirectories method. See here: http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.getdirectories(v=vs.71).aspx
The System.IO namespace contains a lot of tools for this. The Directory.GetDirectories you can use to get al subfolders.
For example:
var folder = folderBrowserDialog1.SelectedPath;
var subFolders = System.IO.Directory.GetDirectories(folder);
Related
FolderBrowserDialog fd = new FolderBrowserDialog();
fd.RootFolder = string.Format("D:\\Project\\folder1\\folder2\\ Results\\{0}", FolderName);
in FolderBrowserDialog the Rootfolder Expects a type of environment.specialfolder
but i want to add my folder as root folder.
i dont want to set the SelectedPath as my custom path.
is there any way to do so.
thanks in advance.
There is no way to change the RootFolder to a custom folder because this is used as a fallback, which I think is what's happening in your code. .Net knows that special folders exist, whereas your custom directory may not.
It looks like you need to remove the space in here ...folder2\\ Results\\... and/or check the FolderName variable as this is producing a directory string that doesn't exist, therefore your dialog is using the RootFolder instead.
Default the RootFolder to Desktop
Set your custom folder as the SelectedPath property
fd.SelectedPath = string.Format("D:\Project\folder1\folder2\ Results\{0}", FolderName);
Call Show method of dialog
fd.ShowDialog();
Note that order of these settings need to be preserved, else will lead to wrong result.
More detail in following answered question
Set folder browser dialog start location
I've dropped a folder browser on a form called "folderBrowserDialog1" and the below seems to work. The special folder option is simply an option to allow the browsing to start in a "Special" folder. E.g. Windows etc.. and it provides a neat mechanism to set it without typing the full path. If you need a custom path, then set the SelectedPath property.
folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
//The above is optional. You don't need to set it.
folderBrowserDialog1.SelectedPath = #"C:\"; //Your path here
folderBrowserDialog1.ShowDialog();
Hope that helps
Is there any efficient way on C# to determine whether exists subfolder with name 'sub' in any level of the folder.
Means, given path I would like to determine if there is any level on the subtree of path which contais folder 'sub'.
Thanks!
You can use Directory.GetDirectories() or Directory.EnumerateDirectories() methods, specifying that includes the current directory and all its subdirectories in a search operation:
var subDirs = Directory.GetDirectories("ROOT PATH", "sub", SearchOption.AllDirectories);
I am pretty new in C# and I am finding some difficulties trying to retrieve a jpg file that is into a directory of my project.
I have the following situation:
I have a Solution that is named MySolution, inside this solution there are some projects including a project named PdfReport. Inside this project there is a folder named Shared and inside this folder there is an header.jpg file.
Now if I want to obtain the list of all files that are inside the Shared directory (that as explained is a directory inside my project) I can do something like this:
string[] filePaths = Directory.GetFiles(#"C:\Develop\EarlyWarning\public\Implementazione\Ver2\PdfReport\Shared\");
and this work fine but I don't want use absolute path but I'd rather use a relative path relative to the PdfReport project.
I am searching a solution to do that but, untill now, I can't found it. Can you help me to do that?
Provided your application's Executable Path is "C:\Develop\EarlyWarning\public\Implementazione\Ver2", you can access the PdfReport\Shared folder as
string exePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
string sharedPath = Path.Combine(exePath, "PdfReport\\Shared\\");
string[] filePaths = Directory.GetFiles(sharedPath);
Try to get the current folder by using this
Server.MapPath(".");
In a non ASP.NET application but WinForms or Console or WPF application you should use
AppDomain.CurrentDomain.BaseDirectory
If you want root relative, you can do this (assuming C:\Develop\EarlyWarning is site root)
string[] filePaths = Directory.GetFiles(Server.MapPath("~/public/Implementazione/Ver2/PdfReport/Shared"));
Or if you want plain relative,
//assuming you're in the public folder
string[] filePathes = Directory.GetFiles(Server.MapPath("/Implementazione/Ver2/PdfReport/Shared"));
Root relative is usually best in my experience, in case you move the code around.
You can right click on your file header.jpg, choose Properties, and select for example the option Copy always on the property "Copy to Output Directory".
Then a method like this, in any class that belongs to project PdfReport:
public string[] ReadFiles()
{
return Directory.GetFiles("Shared");
}
will work well.
Alternatively, if you have files that never change at runtime and you want to have access to them inside the assembly you also can embed: http://support.microsoft.com/kb/319292/en-us
I am developing a desktop application in C#.
I have programmatically created a folder(say ABC) inside the Main folder in C drive in Windows.
I want to know whether an user has created any new folder(by simply right clicking and creating new folder) inside ABC.
If the user has created a new folder then I need to get the details of that folder like folder name and privacy too.
Thanks in advance!
You can get the subdirectories of a folder (in your example, the folder "ABC") as an array of strings by calling the method GetDirectories:
string[] subdirs = Directory.GetDirectories(#"C:\ABC");
Then, if you'd like, you can iterate through all of them:
foreach (string dir in subdirs)
//dir is a path to a subdirectory
Don't forget the using statement!
using System.IO;
You can use DirectoryInfo to get the list of subfolder
DirectoryInfo dirInfo = new DirectoryInfo(#"c:\ABC");
DirectoryInfo[] subFolders = dirInfo.GetDirectories();
I'm not sure what you mean by privacy...
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