I'm trying to make a file explorer with ASP.NET webforms. But when I tried to get selected item in list box, I get an error:
System.NullReferenceException
I tried to make it in Winforms, and it just worked perfectly. I am confused.
Here's some webforms code:
public partial class Default : System.Web.UI.Page
{
DriveInfo[] drives;
string path = "";
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.Items.Clear();
drives = DriveInfo.GetDrives();
foreach (DriveInfo d in drives)
{
ListBox1.Items.Add(d.Name);
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// this is the error section
path = ListBox1.SelectedItem.ToString();
ListBox1.Items.Clear();
string[] dirs = Directory.GetDirectories(path);
string[] files = Directory.GetFiles(path);
foreach(string d in dirs)
{
ListBox1.Items.Add(d);
}
foreach(string f in files)
{
ListBox1.Items.Add(f);
}
TextBox1.Text = path;
}
}
I am a beginner in webforms, and this is my first project.
I am sorry if this is a stupid question.