我制作了一个程序来扫描用户选择的文件夹,它将搜索 MP3 文件和 WAV 文件,然后它会计算这些文件的总大小,但是当它执行所有这些操作时,程序无响应标签将不会更新或只有一半并且窗口不能拖动。
我该如何解决这个问题?
编辑 :
这是检查文件大小的代码:
public string testFileSize(String dir)
{
if (MFILE_OPTION_SUBFOLDER_CHECKBOX.Checked == true)
{
double totalFileSize = 0;
if (MFILE_OPTION_MP3_CHECKBOX.Checked == true)
{
string[] files = System.IO.Directory.GetFiles(dir, "*.mp3", SearchOption.AllDirectories);
foreach (string fileName in files)
{
FileInfo fi = new FileInfo(fileName);
double fileSize = fi.Length;
totalFileSize += fileSize;
}
}
if (MFILE_OPTION_WAV_CHECKBOX.Checked == true)
{
string[] files = System.IO.Directory.GetFiles(dir, "*.wav", SearchOption.AllDirectories);
foreach (string fileName in files)
{
FileInfo fi = new FileInfo(fileName);
double fileSize = fi.Length;
totalFileSize += fileSize;
}
}
totalFileSize = Math.Round((totalFileSize / 1024f) / 1024f, 2);
return totalFileSize.ToString() + " MB";
}
else
{
double totalFileSize = 0;
if (MFILE_OPTION_MP3_CHECKBOX.Checked == true)
{
string[] files = System.IO.Directory.GetFiles(dir, "*.mp3", SearchOption.TopDirectoryOnly);
foreach (string fileName in files)
{
FileInfo fi = new FileInfo(fileName);
double fileSize = fi.Length;
totalFileSize += fileSize;
}
}
if (MFILE_OPTION_WAV_CHECKBOX.Checked == true)
{
string[] files = System.IO.Directory.GetFiles(dir, "*.wav", SearchOption.TopDirectoryOnly);
foreach (string fileName in files)
{
FileInfo fi = new FileInfo(fileName);
double fileSize = fi.Length;
totalFileSize += fileSize;
}
}
totalFileSize = Math.Round((totalFileSize / 1024f) / 1024f, 2);
return totalFileSize.ToString() + " MB";
}
}
这是我尝试过的,但它们具有如上所示的所有文件类型^^
if (MFILE_OPTION_WAV_CHECKBOX.Checked == true)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (s, e) =>
{
string[] files = System.IO.Directory.GetFiles(dir, "*.wav", SearchOption.TopDirectoryOnly);
foreach (string fileName in files)
{
FileInfo fi = new FileInfo(fileName);
double fileSize = fi.Length;
totalFileSize += fileSize;
}
};
bw.RunWorkerAsync();
}
但这如何返回 0 MB?