|
|
|
ช่วยทีค่ะ เกี่ยวกับการใช้ checkBox ค้นหาไฟล์ค่ะ c# |
|
|
|
|
|
|
|
[color=purple]ต้องทำยังไงคะถึงจะ เลือกกรองFileได้ เช่น txt,pic,HTML แบบนี้อ่ะค่ะ
หนูลองทำกับ checkBox ทั้ง 3 อัน ก้อไม่กรองFileให้อ่ะค่ะ c# 2008ค่ะ
พี่ๆใจดีช่วยเขียนโค้ดยกตัวอย่างหรือแนะนำเป็นโค้ดติดต่อกับ DirectoryInfo ให้ทีนะค่ะ
แล้วก้อ จะทำ ค้นหาไฟล์ก้อเขียนโค้ดไม่เป็นอ่ะค่ะ แบบพิมพ์ชื่อไฟล์ แล้วขึ้นมาให้เลย แง่ๆ
แต่ทำไงให้ checkBoxกรอง นามสกุลได้อ้ะคะ ขอบคุณค่ะ [/color]
Code (C#)
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace File_Browser_Pro
{
public partial class Form1 : Form
{
private string currentFolderPath;
private string sampledriveletter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadDrive();
//label10.Visible = true;
}
protected void LoadDrive()
{
DirectoryInfo driveletter;
foreach (char c in sampledriveletter)
{
string drive = c + ":\\";
driveletter = new DirectoryInfo(drive);
if (driveletter.Exists == true)
{
DriveCombo.Items.Add(driveletter.FullName);
}
}
}
protected void ClearAllFields()
{
listBoxFolders.Items.Clear();
listBoxFiles.Items.Clear();
textBoxFolder.Text = "";
textBoxFileName.Text = "";
textBoxCreationTime.Text = "";
textBoxLastAccessTime.Text = "";
textBoxLastWriteTime.Text = "";
textBoxFileSize.Text = "";
currentFolderPath = null;
}
protected void DisplayFileInfo(string fileFullName)
{
FileInfo theFile = new FileInfo(fileFullName);
if (!theFile.Exists)
{
throw new FileNotFoundException("File not found: " + fileFullName);
}
textBoxFileName.Text = theFile.Name;
textBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString();
textBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString();
textBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString();
textBoxFileSize.Text = theFile.Length.ToString() + " bytes";
}
protected void DisplayFolderList(string folderFullName)
{
DirectoryInfo theFolder = new DirectoryInfo(folderFullName);
if (!theFolder.Exists)
{
throw new DirectoryNotFoundException("Folder not found: " + folderFullName);
}
ClearAllFields();
textBoxFolder.Text = theFolder.FullName;
currentFolderPath = theFolder.FullName;
// list all subfolders in folder
foreach (DirectoryInfo nextFolder in theFolder.GetDirectories())
listBoxFolders.Items.Add(nextFolder.Name);
// list all files in folder
foreach (FileInfo nextFile in theFolder.GetFiles())
listBoxFiles.Items.Add(nextFile.Name);
}
private void listBoxFiles_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string selectedString = listBoxFiles.SelectedItem.ToString();
string fullFileName = Path.Combine(currentFolderPath, selectedString);
DisplayFileInfo(fullFileName);
openFile(fullFileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void listBoxFolders_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string selectedString = listBoxFolders.SelectedItem.ToString();
string fullPathName = Path.Combine(currentFolderPath, selectedString);
DisplayFolderList(fullPathName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonUp_Click(object sender, EventArgs e)
{
try
{
string folderPath = new FileInfo(currentFolderPath).DirectoryName;
DisplayFolderList(folderPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
ClearAllFields();
}
private void DriveCombo_SelectedValueChanged(object sender, EventArgs e)
{
try
{
string folderPath = DriveCombo.SelectedItem.ToString();
DirectoryInfo theFolder = new DirectoryInfo(folderPath);
if (theFolder.Exists)
{
DisplayFolderList(theFolder.FullName);
return;
}
FileInfo theFile = new FileInfo(folderPath);
if (theFile.Exists)
{
DisplayFolderList(theFile.Directory.FullName);
int index = listBoxFiles.Items.IndexOf(theFile.Name);
listBoxFiles.SetSelected(index, true);
return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void openFile(string openFileName)
{
if (File.Exists(openFileName))
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = openFileName;
startInfo.Arguments = "";
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void toolStripStatusLabel1_Click(object sender, EventArgs e)
{
}
}
}
Tag : .NET, C#
|
|
|
|
|
|
Date :
2010-09-17 18:00:33 |
By :
nupoychan |
View :
1419 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|