public partial class Form1 : Form {
private FormProgressbar _frmProgressBar; //instance of ProgressBar form.
private delegate bool IncreaseProBarHandler(int val); //delegate to handler the corresponding.
private IncreaseProBarHandler _incProBar; //instance of the handler.
public Form1() {
InitializeComponent();
}
//Show the ProgressBar form.
void ProgressBarFormShow() {
_frmProgressBar = new FormProgressbar();
_incProBar = _frmProgressBar.IncreaseProbarVal;
_frmProgressBar.ShowDialog(); //treat the ProgressBar as modal dialog.
} //Management the ProgressBar form.
void MngProgressBarForm() {
try {
MethodInvoker proBarFormInvo = ProgressBarFormShow;
this.BeginInvoke(proBarFormInvo); Thread.Sleep(100); //sleep for a while to show the window. bool bIncProBar = false; //determine whether increase the ProgressBar successfully.
do {
Thread.Sleep(5); //sleep a while each time trying to increase the ProgressBar value.
bIncProBar = (bool)this.Invoke(_incProBar, new object[] { 2 });
} while(bIncProBar);
}
catch(System.Exception ex) {
MessageBox.Show(ex.Message);
} }
//Store the selected directory.
private void btn_OpenFD_Click(object sender, EventArgs e) {
FolderBrowserDialog folderBD = new FolderBrowserDialog();
if(folderBD.ShowDialog() == DialogResult.OK)
comBoxPath.Text = folderBD.SelectedPath;
}
//Start to read.
private void btnRead_Click(object sender, EventArgs e) {
new Thread(MngProgressBarForm).Start(); //start the ProgressBar Thread.
ArrayList arrDirs = new ArrayList(); //存储路径列表.
ArrayList arrFiles = new ArrayList(); //存储文件列表.
GetFilesList(comBoxPath.Text.Trim(), "*.*", arrDirs, arrFiles, true);
StringBuilder strBuilder = new StringBuilder(arrDirs.Count + arrFiles.Count); //拼接文件/文件名. foreach(var dir in arrDirs) { //路径.
strBuilder.Append(dir + "\n");
} foreach(var file in arrFiles) { //文件.
strBuilder.Append(file + "\n");
} richTxtShow.Text = Convert.ToString(strBuilder);
labCountAll.Text = "总共找到\"" + arrDirs.Count + "\"文件夹, \"" + arrFiles.Count + "\" 文件.";
} //Get File/Directory List.
/// <summary>
/// 读取指定路径下全部"路径/文件"并用列表存储.
/// </summary>
/// <param name="strDir">指定读取的路径.</param>
/// <param name="strFilePattern">读取文件的格式.</param>
/// <param name="arrDirs">存储路径的ArrayList.</param>
/// <param name="arrFiles">存储文件的ArrayList.</param>
/// <param name="bIsRecursive">是否递归读取.</param>
private void GetFilesList(string strDir, string strFilePattern, System.Collections.ArrayList arrDirs, System.Collections.ArrayList arrFiles, bool bIsRecursive) {
//None directory.
if(string.IsNullOrEmpty(strDir)) throw new ArgumentNullException("Directory");
try {
DirectoryInfo dir = new DirectoryInfo(strDir);
string result = ""; //存储与查找文件/文件名相符的文件名/路径名.
//Directory.
string[] directorys = Directory.GetDirectories(strDir);
DirectoryInfo[] directorys =
foreach(var dir in directorys) {
result = Path.GetFileNameWithoutExtension((string)dir).ToLower();
if(result.Contains((string)txtFileSearch.Text.ToLower().Trim()))
arrDirs.Add(dir);
}
//File.
string[] files = Directory.GetFiles(strDir, strFilePattern);
foreach(var f in files) {
result = Path.GetFileNameWithoutExtension((string)f).ToLower();
if(result.Contains((string)txtFileSearch.Text.ToLower().Trim()))
arrFiles.Add(f);
} //Recursive.
if(bIsRecursive) {
foreach(var dir in directorys) {
GetFilesList(dir, strFilePattern, arrDirs, arrFiles, true);
}
}
}
catch(System.Exception ex) {
richTxtShow.Text = ex.Message;
}
} private void Form1_Load(object sender, EventArgs e) {
comBoxPath.SelectedIndex = 0; //初始选项路径.
}
}//我用这个方法(添加进度条)做(拷贝文件或文件夹)的时候都没有问题...这里为什么不行
private FormProgressbar _frmProgressBar; //instance of ProgressBar form.
private delegate bool IncreaseProBarHandler(int val); //delegate to handler the corresponding.
private IncreaseProBarHandler _incProBar; //instance of the handler.
public Form1() {
InitializeComponent();
}
//Show the ProgressBar form.
void ProgressBarFormShow() {
_frmProgressBar = new FormProgressbar();
_incProBar = _frmProgressBar.IncreaseProbarVal;
_frmProgressBar.ShowDialog(); //treat the ProgressBar as modal dialog.
} //Management the ProgressBar form.
void MngProgressBarForm() {
try {
MethodInvoker proBarFormInvo = ProgressBarFormShow;
this.BeginInvoke(proBarFormInvo); Thread.Sleep(100); //sleep for a while to show the window. bool bIncProBar = false; //determine whether increase the ProgressBar successfully.
do {
Thread.Sleep(5); //sleep a while each time trying to increase the ProgressBar value.
bIncProBar = (bool)this.Invoke(_incProBar, new object[] { 2 });
} while(bIncProBar);
}
catch(System.Exception ex) {
MessageBox.Show(ex.Message);
} }
//Store the selected directory.
private void btn_OpenFD_Click(object sender, EventArgs e) {
FolderBrowserDialog folderBD = new FolderBrowserDialog();
if(folderBD.ShowDialog() == DialogResult.OK)
comBoxPath.Text = folderBD.SelectedPath;
}
//Start to read.
private void btnRead_Click(object sender, EventArgs e) {
new Thread(MngProgressBarForm).Start(); //start the ProgressBar Thread.
ArrayList arrDirs = new ArrayList(); //存储路径列表.
ArrayList arrFiles = new ArrayList(); //存储文件列表.
GetFilesList(comBoxPath.Text.Trim(), "*.*", arrDirs, arrFiles, true);
StringBuilder strBuilder = new StringBuilder(arrDirs.Count + arrFiles.Count); //拼接文件/文件名. foreach(var dir in arrDirs) { //路径.
strBuilder.Append(dir + "\n");
} foreach(var file in arrFiles) { //文件.
strBuilder.Append(file + "\n");
} richTxtShow.Text = Convert.ToString(strBuilder);
labCountAll.Text = "总共找到\"" + arrDirs.Count + "\"文件夹, \"" + arrFiles.Count + "\" 文件.";
} //Get File/Directory List.
/// <summary>
/// 读取指定路径下全部"路径/文件"并用列表存储.
/// </summary>
/// <param name="strDir">指定读取的路径.</param>
/// <param name="strFilePattern">读取文件的格式.</param>
/// <param name="arrDirs">存储路径的ArrayList.</param>
/// <param name="arrFiles">存储文件的ArrayList.</param>
/// <param name="bIsRecursive">是否递归读取.</param>
private void GetFilesList(string strDir, string strFilePattern, System.Collections.ArrayList arrDirs, System.Collections.ArrayList arrFiles, bool bIsRecursive) {
//None directory.
if(string.IsNullOrEmpty(strDir)) throw new ArgumentNullException("Directory");
try {
DirectoryInfo dir = new DirectoryInfo(strDir);
string result = ""; //存储与查找文件/文件名相符的文件名/路径名.
//Directory.
string[] directorys = Directory.GetDirectories(strDir);
DirectoryInfo[] directorys =
foreach(var dir in directorys) {
result = Path.GetFileNameWithoutExtension((string)dir).ToLower();
if(result.Contains((string)txtFileSearch.Text.ToLower().Trim()))
arrDirs.Add(dir);
}
//File.
string[] files = Directory.GetFiles(strDir, strFilePattern);
foreach(var f in files) {
result = Path.GetFileNameWithoutExtension((string)f).ToLower();
if(result.Contains((string)txtFileSearch.Text.ToLower().Trim()))
arrFiles.Add(f);
} //Recursive.
if(bIsRecursive) {
foreach(var dir in directorys) {
GetFilesList(dir, strFilePattern, arrDirs, arrFiles, true);
}
}
}
catch(System.Exception ex) {
richTxtShow.Text = ex.Message;
}
} private void Form1_Load(object sender, EventArgs e) {
comBoxPath.SelectedIndex = 0; //初始选项路径.
}
}//我用这个方法(添加进度条)做(拷贝文件或文件夹)的时候都没有问题...这里为什么不行
//the method in the ProgressBar Form:
/// <summary>
/// increase the progressbar value.
/// </summary>
/// <param name="val">the val to increase each time.</param>
/// <returns>true if it increase successfully;otherwise false.</returns>
public bool IncreaseProbarVal(int val) {
if(probar.Value + val < probar.Maximum) {
probar.Value += val;
return true;
}
else {
probar.Value = probar.Maximum;
return false;
}
}
//Management the ProgressBar form.
void MngProgressBarForm() {
try {
MethodInvoker proBarFormInvo = ProgressBarFormShow;
this.BeginInvoke(proBarFormInvo);
Thread.Sleep(100); //sleep for a while to show the window.
bool bIncProBar = false; //determine whether increase the ProgressBar successfully.
do {
Thread.Sleep(5); //sleep a while each time trying to increase the ProgressBar value.
bIncProBar = (bool)this.Invoke(_incProBar, new object[] { 2 });
} while(bIncProBar);
}
catch(System.Exception ex) {
MessageBox.Show(ex.Message);
}
}
我问的不难吧...只是我为了大家方便,全上了代码而已...
我现在主要是搞不懂, Thread.Sleep 中,阻塞是是主线程还是子线程....搞懂这个就没有问题了...