|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionTo use this, you'll need to add 3 Controls to the Toolbox that Microsoft has disabled by default. They are amazing controls that simplify the workload of this project by a lot. They are, DriveListBox, DirListBox, and FileListBox. Simply go to Tools > Choose Toolbox Items > and check DriveListBox, DirListBox, and FileListBox. Once done, add them and go to your design view. Add the 3 new controls to your project (arrange to look good). Using the codeUsing ListBoxes private void driveListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
dirListBox1.Path = driveListBox1.Drive;
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void dirListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
fileListBox1.Path = dirListBox1.Path;
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void fileListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string fullPath = fileListBox1.Path + "\\" + fileListBox1.FileName;
try
{
sr = new StreamReader(fullPath);
richTextBox1.Text = sr.ReadToEnd();
textBox1.Text = fullPath;
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
Copy File
private void button2_Click(object sender, EventArgs e)
{
sr.Close();
try
{
File.Copy(fileListBox1.Path + "\\" + fileListBox1.FileName,
dirListBox2.Path + "\\" + fileListBox1.FileName, true);
richTextBox1.Clear();
richTextBox1.Text += "Copy Succeeded\n";
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
Delete File
private void button1_Click(object sender, EventArgs e)
{
try
{
DialogResult dr;
dr = MessageBox.Show("Are you sure you wish to delete " + fileListBox1.FileName + "?",
"Confirmation",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (dr == DialogResult.No)
return;
sr.Close();
File.Delete(fileListBox1.Path + "\\" + fileListBox1.FileName);
fileListBox1.Refresh();
dirListBox1.Refresh();
dirListBox2.Refresh();
driveListBox1.Refresh();
driveListBox2.Refresh();
richTextBox1.Clear();
richTextBox1.Text += "Delete Succeeded";
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||