Open files from listbox file listing in c#
-
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Probing\Desktop");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
ListBox1.Items.Add(file.Name);
}I have the source code but its not displaying anything in the listbox.. can anyone help me why is this so?
-
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Probing\Desktop");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
ListBox1.Items.Add(file.Name);
}I have the source code but its not displaying anything in the listbox.. can anyone help me why is this so?
If I understand the question correctly, the problem is that you have placed the code that adds items into the listbox inside a SelectedIndexChanged event handler. So the code will only be run when an item from the list box is selected so if you have no items, the code is never run. Try moving the code to a place which is executed when the listbox is shown. For example if this is Windows Forms, you can use load event:
public Form1() {
InitializeComponent();this.Load += Form1_Load;
}private void Form1_Load(object sender, EventArgs e) {
DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Probing\Desktop");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
ListBox1.Items.Add(file.Name);
}
} -
If I understand the question correctly, the problem is that you have placed the code that adds items into the listbox inside a SelectedIndexChanged event handler. So the code will only be run when an item from the list box is selected so if you have no items, the code is never run. Try moving the code to a place which is executed when the listbox is shown. For example if this is Windows Forms, you can use load event:
public Form1() {
InitializeComponent();this.Load += Form1_Load;
}private void Form1_Load(object sender, EventArgs e) {
DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Probing\Desktop");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
ListBox1.Items.Add(file.Name);
}
}ya, its worked. Thank you so much for the kind share.
-
If I understand the question correctly, the problem is that you have placed the code that adds items into the listbox inside a SelectedIndexChanged event handler. So the code will only be run when an item from the list box is selected so if you have no items, the code is never run. Try moving the code to a place which is executed when the listbox is shown. For example if this is Windows Forms, you can use load event:
public Form1() {
InitializeComponent();this.Load += Form1_Load;
}private void Form1_Load(object sender, EventArgs e) {
DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Probing\Desktop");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
ListBox1.Items.Add(file.Name);
}
}I have one more question. is the files can be download to local from listbox? I have looked for it but I did not get any source code for it.
-
I have one more question. is the files can be download to local from listbox? I have looked for it but I did not get any source code for it.
-
I'm sorry but I don't quite understand the question. Also this seems to be a separate issue, so I suggest posting a new question with relevant details.
ok. thank you