リストボックスがあり、ディレクトリフォルダーからロードされたファイルがいくつかあります。
ファイルを listBox1 にロードするコード:
private void Form1_Load(object sender, EventArgs e)
{
PopulateListBox(listbox1, @"C:\TestLoadFiles", "*.rtld");
}
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
FileInfo[] Files = dinfo.GetFiles(FileType);
foreach (FileInfo file in Files)
{
lsb.Items.Add(file.Name);
}
}
フォームのラベルに属性値を読み取って表示したい。に読み込まれたファイル。listBox1
コードは次のとおりです。
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string path = (string)listBox1.SelectedItem;
DisplayFile(path);
}
private void DisplayFile(string path)
{
string xmldoc = File.ReadAllText(path);
using (XmlReader reader = XmlReader.Create(xmldoc))
{
while (reader.MoveToNextAttribute())
{
switch (reader.Name)
{
case "description":
if (!string.IsNullOrEmpty(reader.Value))
label5.Text = reader.Value; // your label name
break;
case "sourceId":
if (!string.IsNullOrEmpty(reader.Value))
label6.Text = reader.Value; // your label name
break;
// ... continue for each label
}
}
}
}
Problem:
フォームが読み込まれた後に listBox1 のファイルをクリックすると、ファイルはフォルダーからリストボックスに読み込まれますが、エラーがスローされますFile not found in the directory
。
どうすればこの問題を解決できますか???