Hintergrundarbeiter, die eine Methode aufruftC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Hintergrundarbeiter, die eine Methode aufruft

Post by Anonymous »

Ich habe einen Hintergrundarbeiter, der tatsächlich eine Methode (DirectoryMethod) aufruft, die den OrdnerBrowserDialog öffnet und einen Hash für alle Dateien im Verzeichnis durchführt. Sobald der Hash abgeschlossen ist, sind die Dateien in einer Listenansicht mit ihrem entsprechenden Hash- und Dateigröße aufgeführt. Jede Hilfe wird sehr geschätzt. Dies ist in Windows -Formularen gegen 2022. < /P>

Code: Select all

    private void btnBrowse_Click(object sender, EventArgs e)
{
backgroundWorker.RunWorkerAsync();
}

private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
try
{
this.Invoke(new MethodInvoker(delegate
{
// Select the Hash Algorithm
string HashOption = Convert.ToString(comboBox1.SelectedIndex);
// Select how many file to hash; up to 500.
int FileCount;
FileCount = int.Parse(cobFileCount.SelectedItem.ToString());
if (cbIncludeSubs.Checked == true)
{
// Get Top Directory and Sub Directories as well as how many you want.
// Up to 500 files can be loaded.
DirectoryMethod(HashOption, 0, FileCount);
//
}
else if (cbFileDir.Checked == true)
{
// Get all file and subdirectories.
DirectoryMethod(HashOption, 1, FileCount);
//
}
else
{
// Get only files in the Top Directory.
// Only get up to 500 files can be loaded.
DirectoryMethod(HashOption, 1, FileCount);
}
}));
}
catch (Exception exp)
{

MessageBox.Show(exp.Message, "HASHING DIRECTORY ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
//
private void DirectoryMethod(string option, int Directories, int FileCount)
{
try
{
/* Add the zero to "int Directories" gets Top Directory and Sub Directories files.
* Without the zero, gets only the Top Directory files.
* This Method also adds a file counter that only permits upto 500 files to be hashed
* at one time.  This will keep the user from frezzing the program because on the hashing
* algorithm.*/
//
FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog();
// Set description and root folder for Dialog.
FolderBrowserDialog1.Description = "Select a Folder to HASH it's Files";
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
string strSelectedPath;
if (FolderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
strSelectedPath = FolderBrowserDialog1.SelectedPath;
txtFile.Text = FolderBrowserDialog1.SelectedPath;
// Clear the listview even if there's nothing there.
lv.Items.Clear();
string[] filePaths;
if (Directories == 0)
{
filePaths = Directory.GetFiles(strSelectedPath, cbFileType.Text, SearchOption.AllDirectories).Take(FileCount).ToArray();
var selection = option;
int ItemsCount = filePaths.Count();
for (int i = 0; i < ItemsCount; i++)
{
string name = filePaths[i];
// Get file to compute the file size.
FileInfo FileVol = new FileInfo(name);
ListViewItem LI = new ListViewItem(name);
LI = this.lv.Items.Add(name);
// column Hash
LI.SubItems.Add(HashClass.StringFromFilesDirSubsRoutines(name, selection));
string s = HashClass.GetFileSize((int)FileVol.Length);
// Column File Size
LI.SubItems.Add(s);
// File Count
lblCount2.Text = "Count: " + i + 1 + "/" + ItemsCount;
}
}
else
{
filePaths = Directory.GetFiles(strSelectedPath, cbFileType.Text, SearchOption.TopDirectoryOnly).Take(FileCount).ToArray();
var selection = option;
int ItemsCount = filePaths.Count();
foreach (string name in filePaths)
{
ListViewItem LI = new ListViewItem(name);
LI = this.lv.Items.Add(name);
// column Hash
LI.SubItems.Add(HashClass.StringFromFilesDirSubsRoutines(name, selection));
string s = HashClass.GetFileSize((int)name.Length);
// Column File Size
LI.SubItems.Add(s);
// File Count
lblCount2.Text = "Count: " + ItemsCount.ToString();
}
}
txtFile.Text = FolderBrowserDialog1.SelectedPath;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Ich danke Ihnen für Ihre Hilfe im Voraus.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post