About
A directory or a folder can contains multiple directories or files. If you want to access all those directories which is under in a parent directory Then you should use DirecotryInfo class for accessing subDirectories of a parent Directory.In previous example we had seen that how to create a subDirectory under parent Directory.Now in this example we will seen that how to access all subDirectories under parent Directory.Logic behind the program is
Step-1 : Create a instance of DirectoryInfo Class with parent directory path.Step-2 : Now check , If parent directory is exist or not in specified path
Step-3: If Exist then get all subdirectories under parent directory and assign to an array using GetDirectories() method
Step-4 : Items add into List.
Note : GetDirectories() method does not access compressed directories
Lets take an example How to access subDirectories
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string dirpath = textBox1.Text;
DirectoryInfo dirinfo = new DirectoryInfo(dirpath);
if (dirinfo .Exists)
{
DirectoryInfo[] subdir = dirinfo.GetDirectories();
foreach (DirectoryInfo item in subdir)
{
listBox1.Items.Add(item);
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string dirpath = textBox1.Text;
DirectoryInfo dirinfo = new DirectoryInfo(dirpath);
if (dirinfo .Exists)
{
DirectoryInfo[] subdir = dirinfo.GetDirectories();
foreach (DirectoryInfo item in subdir)
{
listBox1.Items.Add(item);
}
}
}
}
}
Output
Tidak ada komentar:
Posting Komentar