Reputation: 53
Part of my application will hopefully be able to hash files which the user specifies, but I am stuck on the actual code itself. Note that:
filepath512(.text)
= Textbox in which the user inserts the file path
fileout(.text)
= Output textbox
button21_click
= "Hash/confirm" button used to start the hashing algorithm
When I run the application and run the hashing algorithm, nothing happens (the result does not appear in the Output textbox). A few weeks ago, I actually successfully executed the hashing algorithm with the identical code (well, same structure) and it worked perfectly well! I have just begun working with C#, so please excuse any messy code!
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string filePath = e.Argument.ToString();
byte[] buffer;
int bytesRead;
long size;
long totalBytesRead = 0;
using (Stream file = File.OpenRead(filePath))
{
size = file.Length;
using (HashAlgorithm hasher = SHA512.Create())
{
do
{
buffer = new byte[4096];
bytesRead = file.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
hasher.TransformBlock(buffer, 0, bytesRead, null, 0);
backgroundWorker1.ReportProgress((int)((double)totalBytesRead / size * 100));
}
while (bytesRead != 0);
hasher.TransformFinalBlock(buffer, 0, 0);
e.Result = MakeHashString(hasher.Hash);
}
}
}
private static string MakeHashString(byte[] hashbytes)
{
StringBuilder hash = new StringBuilder(32);
foreach (byte b in hashbytes)
hash.Append(b.ToString("X2").ToLower());
return hash.ToString();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar3.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
fileout512.Text = e.Result.ToString();
progressBar3.Value = 0;
}
private void button21_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync(filepath512.Text);
}
Upvotes: 0
Views: 224
Reputation: 23675
Actually, your code misses a lot of sanity checks. First of all, you should use OpenFileDialog
instead of a direct user input for specifying a path. Second, once the process starts, you should make sure the file exists using File.Exists
method... if it doesn't, you should return an appropriate result.
Probably, an exception is being thrown by your code, somewhere. From the official MSDN documentation:
If the operation completes successfully and its result is assigned in the DoWork event handler, you can access the result through the RunWorkerCompletedEventArgs.Result property.
[...]
Your RunWorkerCompleted event handler should always check the Error and Cancelled properties before accessing the Result property. If an exception was raised or if the operation was canceled, accessing the Result property raises an exception.
So check error details using the Error
properties of the event arguments in order to make sure that your code was properly executed without exceptions. If this is the case, you must fix the code so that the hashing doesn't fail anymore.
Upvotes: 1
Reputation: 145
does the file exists? you have to check whether the file exists always before trying to do operations. debug the filepath and see whether it exist, and it has read permissions.
Upvotes: 0