how to update a global list of items from different methods

  • Thread starter Thread starter Zolfaghar
  • Start date Start date
Z

Zolfaghar

Guest
In the code below, I seem to be able to collect some of the URIs in my favorites folder, but I am not doing a good job of keeping a comprehensive list called totalUris. Any help is greatly appreciated.

{

/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{

public Window1()
{

InitializeComponent();

string favorites = @"C:\Users\zohal\Favorites";
List<Uri> totalUris = new List<Uri>();

if (File.Exists(favorites))
{
// This path is a file
ProcessFile(favorites, out totalUris);
foreach (Uri address in totalUris)
{
textBox1.AddItem(new AutoCompleteEntry("address.ToString()",null));
}
}
else if (Directory.Exists(favorites))
{
// This path is a directory
ProcessDirectory(favorites);
foreach (Uri address in totalUris)
{
textBox1.AddItem(new AutoCompleteEntry("address.ToString()", null));
}
}
else
{
foreach (Uri address in totalUris)
{
textBox1.AddItem(new AutoCompleteEntry("address.ToString()", null));
}

}
foreach (Uri address in totalUris)
{
textBox1.AddItem(new AutoCompleteEntry("address.ToString()", null));
}
textBox1.AddItem(new AutoCompleteEntry("Toyota Camry", "Toyota Camry", "camry", "car", "sedan"));
textBox1.AddItem(new AutoCompleteEntry("Toyota Corolla", "Toyota Corolla", "corolla", "car", "compact"));
textBox1.AddItem(new AutoCompleteEntry("Toyota Tundra", "Toyota Tundra", "tundra", "truck"));
textBox1.AddItem(new AutoCompleteEntry("Chevy Impala", null)); // null matching string will default with just the name
textBox1.AddItem(new AutoCompleteEntry("Chevy Tahoe", "Chevy Tahoe", "tahoe", "truck", "SUV"));
textBox1.AddItem(new AutoCompleteEntry("Chevrolet Malibu", "Chevrolet Malibu", "malibu", "car", "sedan"));
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.

public static void ProcessDirectory(string targetDirectory)
{
List<Uri> favoriteUrisInCurrentDirectory = new List<Uri>();
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
//if (fileName == "desktop.ini") ???
//cumulativeUris.AddRange(favoriteUrisInCurrentDirectory = ProcessFile(fileName));
ProcessFile(fileName, out favoriteUrisInCurrentDirectory);
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);

}
// Insert logic for processing found files here.
public static void ProcessFile(string theFile, out List<Uri> totalUris)
{
Console.WriteLine("Processed file '{0}'.", theFile);
// Add the file to the list of internet shortcut files
StreamReader reader = File.OpenText(theFile);
System.String line;
List<Uri> favoriteUris = new List<Uri>();
while ((line = reader.ReadLine()) != null)
{
//Go through the file, and match any URIs, and add them to a list
Regex r = new Regex(@"(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*");
//Regex r = new Regex (@"http(s) ?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$");
//Regex r = new Regex(@"http(s) ?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$");
//Regex r = new Regex(@"(((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&amp;%\$#\=~_\-]+))*$");
// Match the regular expression pattern against a text string.
Match m = r.Match(line);
Uri currentUri;

while (m.Success)
{
currentUri = new Uri(m.ToString());
favoriteUris.Add(currentUri);
m = m.NextMatch();

}

}
totalUris = favoriteUris;
//while ((line = reader.ReadLine()) != null)
//{
// string[] items = line.Split('\n',' ');

// foreach (string item in items)
// {
// currentUri = new Uri(item);
// if (currentUri.IsWellFormedOriginalString())
// favoriteUris.Add(currentUri);
// }

//}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = string.Empty;
}
}
}

Continue reading...
 
Back
Top