PDA

View Full Version : سوال: ساخت Dir در C#



mvb_mehran
سه شنبه 20 تیر 1391, 18:57 عصر
:::سلام::::لبخندساده:
سوالم اینه که چجوری میتونم تو یک listbox مثلا فولدرها و فایل های درایو x و نمایش بدم .:متفکر: و بتونم تو فولدر ها پیمایش کنم.
در ضمن میخواستم آدرس فولدری که توش قرار دارم و در یه label نمایش بدم.
:::ممنون::::قلب:

the king
سه شنبه 20 تیر 1391, 22:22 عصر
:::سلام::::لبخندساده:
سوالم اینه که چجوری میتونم تو یک listbox مثلا فولدرها و فایل های درایو x و نمایش بدم .:متفکر: و بتونم تو فولدر ها پیمایش کنم.
در ضمن میخواستم آدرس فولدری که توش قرار دارم و در یه label نمایش بدم.
:::ممنون::::قلب:
یک ListBox و یک Label روی فرم Form1 قرار دهید و کد های زیر رو داخل فرم اضافه کنید.

public Form1()
{
InitializeComponent();
this.Load += new System.EventHandler(this.Form1_Load);
this.listBox1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.listBo x1_MouseDoubleClick);
}

private void Form1_Load(object sender, EventArgs e)
{
BrowsePath("C:\\");
}

private void BrowsePath(string path)
{
path = System.IO.Path.GetFullPath(path);
label1.Text = path;
string[] folders = System.IO.Directory.GetDirectories(path);
string[] files = System.IO.Directory.GetFiles(path);
listBox1.BeginUpdate();
listBox1.Items.Clear();
if (System.IO.Path.GetPathRoot(path) != path)
{
listBox1.Items.Add("[..]");
}
foreach (string folder in folders)
{
listBox1.Items.Add("[" + System.IO.Path.GetFileName(folder) + "]");
}
foreach (string file in files)
{
listBox1.Items.Add(System.IO.Path.GetFileName(file ));
}
listBox1.EndUpdate();
}

private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (listBox1.SelectedIndex >= 0)
{
string item = listBox1.Text;
if (listBox1.Text.StartsWith("["))
{
string folder = System.IO.Path.Combine(label1.Text, item.Substring(1, item.Length - 2));
if (System.IO.Directory.Exists(folder))
{
BrowsePath(folder);
}
}
}
}