المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Searching for a file in multiple directories?



C# Programming
03-10-2013, 11:13 AM
Hey Guys, I am kind of looking around at a couple different places for answers here, got some help from some great folks here before so here goes!

I am working on, same project as I have always been working on, just adding more stuff lol. In this project I want to call an ini setting into a combo box. Here's where I am, I can list the directories into a comboBox, and I can read a line from an ini from a single ******** into a combo box. Now, I need to take this one step further. I need to list that ini setting in the comboBox, from within all those directories. All ini files are named the same thing, but the context is different in the ini field. So basically, I have two pieces two a puzzle figured out, that I need to make fit. Here's an example of what I can do, and what I want it to do.

//I can list the directories into a combo box using this
//Inside the FOLDERS directory are three folders, Folder1, Folder2, and Folder3.
//They are what is displayed in the comboBox.
DirectoryInfo obj = new DirectoryInfo("C:\\Users\\first.last\\Desktop\\Projects\\Test Files\\Folders\\");//you can set your directory path here
DirectoryInfo[] folders = obj.GetDirectories();
comboBox.DataSource = folders; // Populate combobox

Ok, so that's simple enough. What I want to do is take the values of my choice from the code below and list them from EACH directory into the comboBox. There could be any numbers of folders in that directory, but that shouldn't matter.

Here is how I show my ini settings now

IniFile ini = new IniFile("C:\\Users\\first.last\\Desktop\\Projects\\Test Files\\Folders\\Folder1\\Settings.txt");
comboBox.Text = ini.IniReadValue("Info", "Name");

Here's what I WANT to be populated in the comboBox at time of load.

IniFile ini = new IniFile("C:\\Users\\first.last\\Desktop\\Projects\\Test Files\\Folders\\" + Search.All.Folders.For + "\\Settings.txt");
comboBox.Text = ini.IniReadValue("Info", "Name");

I am not sure if that is even how it would be done, but I want to list that value in the combo box, for each folder that has that settings.txt.


If I need to clarify anything, please let me know, I can also provide a complete example of my source code if someone needs it zipped up for them. Thank you!


EDIT:

So I see I have gotten quite a few views of this and maybe it is a bit confusing. Here is how I theorize this happening, but needing guidance on making it a reality.

Couldn't I define folders as a string, and do something like
For Each (Folder) //(meaning it will do this block of code for each item in the string, being the folders in the specified directory)
{
IniFile ini = new IniFile("C:\\Users\\first.last\\Desktop\\Projects\\Test Files\\Folders\\" + Folder + "\\Settings.txt");
comboBox.Text = ini.IniReadValue("Info", "Name");
}

Edit #2 - I am going back through my C# tutorials because I remembered something that I learned about the while statement, I thought I could apply it in this case but am having no luck making "folders" turn into a string to even remotely make this possible. I am referring to www.learnvisualstudio.net video number 2010_02_06.
Here was my theory....

string thisFolder = folders; //I can't make this a string because it is already declared and I can't convert it to a sting implicitly...
while (folders != null)
{
//execute
IniFile ini = new IniFile("C:\\Users\\first.last\\Desktop\\Projects\\Test Files\\Folders\\" + thisFolders + "\\Settings.txt");
if (folders = null)
{
comboBox.Text = ini.IniReadValue("Info", "Name");
}
}

In my head this sounded as if it would have worked. But this just isn't the way things work lol...