So I've used this site for some VBA stuff I did in the past and found some great help by other users' questions. Now I've begun using C# and am totally lost on how to get the details from a file. I will admit, I am completely new to C# and am having a pretty difficult time getting used to it.
What I'm trying to do is choose a directory (already have that in place with FolderBrowserDialog), and then grab all the names of the folders in that directory, and all of the subfolders.
To put it in to context, I want to go to my Music folder, and then be able to make a list of all the artists and albums in that directory in a textbox. Each album will be under the Artist folder, so the whole thing would read the name of one folder, then all the subfolders in that folder, then go back and move on to the next one.
Not sure if anyone will ask, but here is all the code I have:
public Form1()
{
InitializeComponent();
}
private void DirButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK);
{
DirBox.Text = fbd.SelectedPath;
}
}
Any help is greatly appreciated and I'll try to understand your answer(s) as best as I can.
Thanks!
One of the many options would be to use Directory.GetDirectories, see docs
Your code could look something like this:
string[] dirs = Directory.GetDirectories(fbd.SelectedPath, "*", SearchOption.AllDirectories);
Depending on the structure of your folders, you might need to use another SearchOption Enumeration - use 'AllDirectories' to do a recursive search, or TopDirectoryOnly to look only in the top directory, see docs
Now, if you don't need a recursive search you can use a simpler overload DirectoryInfo.GetDirectories as described here, sample follows:
string[] dirs = Directory.GetDirectories(fbd.SelectedPath);
Please note that you can do the same by using DirectoryInfo class instead of Directory class if you prefer to have an instance rather than working with statics, be sure to check out the docs
Related
I have an alfresco database with a directory of folders and files.
I want to be able to move from one specific file to a different folder in the click of a button, so I used winForms for my process
You need to be Using the PortCMIS library in order to test these operations in C# code, there's also a version called "OpenCMIS" you can use if you are using Java.
'''
private void btnMove_Click(object sender, EventArgs e)
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
//Creates instance where I can access
//my private Alfresco sandbox database.
parameters[SessionParameter.BindingType] = BindingType.AtomPub;
parameters[SessionParameter.AtomPubUrl] = txtLink.Text;
parameters[SessionParameter.User] = txtUser.Text;
parameters[SessionParameter.Password] = txtPass.Text;
SessionFactory factory = SessionFactory.NewInstance();
session = factory.GetRepositories(parameters)[0].CreateSession();
//Error here, can't convert the string to
//"PortCMIS.Client.IFolder or IDocument".
IFolder sourcefolder = #"Rootfolder/folderWithFile";
IFolder targetFolder = #"Rootfolder/folderToPutFileIn";
IDocument doc = "fileTest.txt";
//Supposed to move the document from source folder to
//the target folder.
doc.Move(sourceFolder, targetFolder);
}
'''
Example Picture of the Folder directory
I also tried using "IFolder sourcefolder = session.GetRootFolder(#"Rootfolder/folderWithFile");" but it still doesn't work, I'm unsure on what to do
My problem here is the variables I am calling "IFolder" and "IDocument". What should I do for it to do what I want? Do I need to find a way to convert the string directory into what i wants it to be? Is there a specific function that I should be using to help move these files?
Also, not to give excuses but I'm pretty new at using CMIS and Alfresco, and programming in general (I have almost 1 year of experience in coding at this point in time).
Which comes to say, if there is anything I said that might have been wrong, or something I'm doing that makes no sense, please do point it out so I can learn and get better as a result. And if you have any questions that you need answered in order to give a proper answer to my problem (necessary details and information) please do tell me about it.
Thank you !
I'm using C# to get the exact path of the specific folder in windows system by giving the folder name. Is their any way to get the folder path by giving the folder name, where the folder name will be unique.
Update:
Folder is created at run time with current time as the name. This
process is done by the application. Here i know the folder name but i
didn't know path, because path is selected by the user during
installation and installation is done before very long time.
That changes the question considerably. Why not use the application to tell you where it lives:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx
I had a similar idea ages ago and wrote about it as a Code Project Tip:
http://www.codeproject.com/Tips/132804/Open-folders-using-a-Run-Command
Otherwise you would need to index every folder on the PC and make them unique names and look up the full path that way.
The other suggestion I have is using LogParser as the Most efficient way to find all exe files on disk using C#? Its a free Microsoft product but I'm not sure about re-dist permissions, I had to include it in my package separately last time I used it. It full on flys, faster than a speeding train!
I found a Log Parser example that finds folders, you could try it out and adapt it if its useful:
SELECT TOP 1 * FROM C:\TFS\Project\*.* WHERE INDEX_OF(Path, 'Database') > 0
The good folks over at http://visuallogparser.codeplex.com/ have
provided us with the source code.
Open the VisualLogParser solution in VS2010, ignore the prompt about debugging, after the solution loads, F5, set the combo-box to FS (FileSystem), paste in this query and press go.
You could probably use something like this, but it'll be rather slow, depending on how many folders needed to be looked through.
Use it like FindFullPath(rootFolder, folderNameToLookFor)
public static string FindFullPath(string path, string folderName)
{
if (string.IsNullOrWhiteSpace(folderName) || !Directory.Exists(path))
{
return null;
}
var di = new DirectoryInfo(path);
return findFullPath(di, folderName);
}
private static string findFullPath(DirectoryInfo directoryInfo, string folderName)
{
if (folderName.Equals(directoryInfo.Name, StringComparison.InvariantCultureIgnoreCase))
{
return directoryInfo.FullName;
}
try
{
var subDirs = directoryInfo.GetDirectories();
return subDirs.Select(subDir => findFullPath(subDir, folderName)).FirstOrDefault(fullPath => fullPath != null);
}
catch
{
// DirectoryNotFound, Security, UnauthorizedAccess
return null;
}
}
See following link
string dirName = new DirectoryInfo(#"c:\projects\roott\wsdlproj\devlop\beta2\text").Name;
I have posted - How to use OpenFileDialog to select a folder?, I couldn't find the correct answer.
So, I have changed my question.
I want to customize OpenFileDialog to select multiple folders and files. I tried to find a solution and could see some posts about it.
From the internet, I found the following project - https://github.com/scottwis/OpenFileOrFolderDialog.
However, while using this, I faced one problem. It uses the GetOpenFileName function and OPENFILENAME structure from MFC.
And OPENFILENAME has the member named "templateID".
It's the identifier for dialog template. And the sample project has the "res1.rc" file and, also have the templated dialog in it.
But I don't know How can I attach this file to my C# project?
Or is there any other perfect solution about - "How to customize OpenFileDialog to select multiple folders and files?"?
If you use the FileNames property instead of the FileName property, you get a string array of each file selected, you select multiple files using the shift key. Like so:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog x = new OpenFileDialog();
x.Multiselect = true;
x.ShowDialog();
string[] result = x.FileNames;
foreach (string y in result)
MessageBox.Show(y, "Selected Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
For files and folders you need to use the CommonOpenFileDialog included with the WinAPI, the particular class is here.
Try this:
openFileDialog.Multiselect = true;
You might not get a built in .Net control like that. Definitely the OpenFileDialog can not function as both File as well as Folder browser. You have two choices go for a third party tool like the one you found second make your own control. Surprisingly you might not find creating a very simple version of your own control very difficult.
I have posted - How to use OpenFileDialog to select a folder?, I couldn't find the correct answer.
So, I have changed my question.
I want to customize OpenFileDialog to select multiple folders and files. I tried to find a solution and could see some posts about it.
From the internet, I found the following project - https://github.com/scottwis/OpenFileOrFolderDialog.
However, while using this, I faced one problem. It uses the GetOpenFileName function and OPENFILENAME structure from MFC.
And OPENFILENAME has the member named "templateID".
It's the identifier for dialog template. And the sample project has the "res1.rc" file and, also have the templated dialog in it.
But I don't know How can I attach this file to my C# project?
Or is there any other perfect solution about - "How to customize OpenFileDialog to select multiple folders and files?"?
If you use the FileNames property instead of the FileName property, you get a string array of each file selected, you select multiple files using the shift key. Like so:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog x = new OpenFileDialog();
x.Multiselect = true;
x.ShowDialog();
string[] result = x.FileNames;
foreach (string y in result)
MessageBox.Show(y, "Selected Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
For files and folders you need to use the CommonOpenFileDialog included with the WinAPI, the particular class is here.
Try this:
openFileDialog.Multiselect = true;
You might not get a built in .Net control like that. Definitely the OpenFileDialog can not function as both File as well as Folder browser. You have two choices go for a third party tool like the one you found second make your own control. Surprisingly you might not find creating a very simple version of your own control very difficult.
What's the best way to let a user pick a subdirectory in C#?
For example, an app that lets the user organize all his saved html receipts. He most likely is going to want to be able to select a root subdirectory that the program should search for saved webpages (receipts).
Duplicate:
Browse for a directory in C#
The Folder Browser Dialog is the way to go.
If you want to set an initial folder path, you can add this to your form load event:
// Sets "My Documents" as the initial folder path
string myDocsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
FolderBrowserDialog1.SelectedPath = myDocsPath;
Check the FolderBrowserDialog class.
// ...
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folderBrowserDialog1.SelectedPath;
}
FolderBrowserDialog works just fine for this purpose.
FolderBrowserDialog works, but offers very little customization.
If you want a textbox where users can type in the path have a look here
Dupe of:
Browse for a directory in C#
Whatever you do, don't use the FolderBrowserDialog.
Just kidding. Use that.