C# match and export based on listbox textfile and wildcard
I like to thank stackoverflow member AlbertEin for helping with this code. What I have is a file being created from a massive directory listing, and i also have a listbox full of items that are only part of the file name in the directory listing. What I want to do is write out a text file for every listboxitem in listbox1.item based on the item and a wildcard
Example Listbox:
A开发者_开发知识库pple OrangeResults Apple.txt:
Apple13.txt Apple15.txt Apple19.txtand just do this foreach item in the listbox. I'd also like to only read the master txt file once to save me from reading it everytime. Any help appricated.
Better explaination:
I have a listbox of 100 items, and 1 master text file that was created.
For all items in the listbox that match the master test file, I want to write out the matching contents of the master text file to a new textfile with the listboxs name.
The listbox items needs a wildcard in order to match the master text file.
Try this, i don't know what kind of wildcard do you want to use, so i'm only checking that the line starts like the listbox item:
//Popullate listItems from your ListBox
var listItems = new string[] { "Apple", "Orange", "Pineapple" };
var writers = new StreamWriter[listItems.Length];
for (int i = 0; i < listItems.Length; i++)
{
writers[i] = File.CreateText(listItems[i] + ".txt");
}
var reader = new StreamReader(File.OpenRead(bigFatFile));
string line;
while ((line = reader.ReadLine()) != null)
{
for (int i = 0; i < listItems.Length; i++)
{
if (line.StartsWith(listItems[i]))
writers[i].WriteLine(line);
}
}
reader.Close();
foreach (var writer in writers)
writer.Close();
精彩评论