Display A Directory List In A TextBox Control
Display A Directory List In A TextBox Control
First complete the following steps:
Create a new C# Windows Application in Visual Studio.
Add a button control to the form.
Add a TextBox Control to the form.
Change the Multiline Property of the TextBox Control to ‘True’.
Change the ScrollBars Property of the TextBox Control to ‘Vertical’.
Resize the TextBox Control to fit the Form
Double Click on the Button Control, and add the code below to the Click Event.
private void button1_Click(object sender, EventArgs e)
{
//Replace the directory below with a directory on your computer.
string strDirLocal = @"C:\documents\CodeExamples";
if (System.IO.Directory.Exists(strDirLocal))
{
foreach (string sPath in System.IO.Directory.GetFiles(strDirLocal, "*.*"))
{
//Add the file to the TextBox, and remove the Path from the sPath string,
//leaving the file name.
textBox1.Text = textBox1.Text + sPath.Replace(strDirLocal + @"\","") + "\r\n";
}
}
}
#CSharp #VisualStudio