У меня возникли некоторые проблемы со следующим кодом, и мне нужна помощь, чтобы он работал для строк URI.
Это работает, когда я набираю a
, b
, ... Я знаюмой List<URI>
собирает адреса, но автозаполнение не работает с адресами.
Я сделал отладку кода, и ProcessFile()
, кажется, работает нормально, сопоставляет URI и собирает их.Я видел там семь адресов в одной точке.
Но экземпляр totalUris
в операторе col.Add()
пуст.Я не знаю, почему список totalUris
не заполняется.Все это работает довольно медленно.Я видел похожую функциональность для WPF, и они использовали ключевые слова.
Интересно, должен ли я сделать это и здесь, и как это будет работать.Это любой учебник для начинающих, который очень важен для меня.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void addItems(AutoCompleteStringCollection col)
{
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);
col.Add(totalUris.ToString());
}
else if (Directory.Exists(favorites))
{
// This path is a directory
ProcessDirectory(favorites,col);
col.Add(totalUris.ToString());
}
else
{
Console.WriteLine("No file or directory to process");
}
col.Add(totalUris.ToString());
col.Add("Abel");
col.Add("Bing");
col.Add("Catherine");
col.Add("Varghese");
col.Add("John");
col.Add("Kerry");
}
public static void ProcessDirectory(string targetDirectory, AutoCompleteStringCollection col)
{
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,col);
}
// 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\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$");
// 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;
}
private void personalUrisTextBox_TextChanged(object sender, EventArgs e)
{
personalUrisTextBox.AutoCompleteMode = AutoCompleteMode.Suggest;
personalUrisTextBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
addItems(DataCollection);
personalUrisTextBox.AutoCompleteCustomSource = DataCollection;
}
}
}