I am new to C# and Windows Forms.
I am using syncfusion C# library to encrypt / decrypt Word files.
I am using Async / Await tasks to decrypt files in bulk.
I have no problem to encrypt / decrypt one file or to encrypt multiple files.
I encountered the problem when trying to decrypt multiple word files or in bulk.
When decrypting one file with a wrong password, I get an exception and everything works fine.
But during bulk files decryption using await Task.WhenAll(tasks)
inside try-catch block, I am not getting any exception while from the first task (first file to decrypt) I should get an exception because the password is wrong.
During entire week I tried most of the provided solutions everywhere but not successful and I don’t know what I have to do.
Thank you very much!
The code should throw an exception when the first task is not successful but it’s not.
GetPathOrExtention gte = new GetPathOrExtention();
private void DecryptDOCXFiles(string fileName)
{
FileStream inputStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
WordDocument document = new WordDocument(inputStream, FormatType.Docx, TbDecPwd1.Text);
FileStream outputStream = new FileStream(gte.GetDirPath(fileName) + "\" + gte.GetfileName(fileName).Replace("Encrypted-", ""), FileMode.Create, FileAccess.Write);
document.Save(outputStream, FormatType.Docx);
inputStream.Close();
outputStream.Close();
}
private async Task DecryptWordFiles()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "docx files (*.docx)|*.docx|All files (*.*)|*.*";
ofd.Multiselect = true;
var tasks = new List<Task>();
if (TbDecPwd1.Text != "" && ofd.ShowDialog() == DialogResult.OK)
{
foreach (string file in ofd.FileNames)
{
if (gte.GetFileExtension(file) != ".docx")
{
LblProceeding.ResetText();
MessageBox.Show("Files version is not supported!" + 'n' + 'n' + "Only Word version 2019 and above are supported!", "P-GEN", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (gte.GetFileExtension(file) == ".docx")
{
LblProceeding.Text = "Proceeding... Do not exit the software!";
var task = Task.Run(() => DecryptDOCXFiles(file)).ContinueWith(p => progressBar1.PerformStep());
tasks.Add(task);
}
}
}
try
{
await Task.WhenAll(tasks);
LblProceeding.Text = "Done!";
MessageBox.Show("Files Successfully Decrypted!", "P-GEN", MessageBoxButtons.OK, MessageBoxIcon.Information);
LblProceeding.ResetText();
progressBar1.Visible = false;
}
catch (AggregateException)
{
LblProceeding.ResetText();
MessageBox.Show("Ensure the password is correct!" + 'n' + 'n' + "Ensure the files you want to encrypt are not opened in another software!", "P-GEN", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
catch (Exception)
{
LblProceeding.ResetText();
MessageBox.Show("Ensure the password is correct!" + 'n' + 'n' + "Ensure the files you want to encrypt are not opened in another software!", "P-GEN", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
private void BtnWordFilesDec_Click(object sender, EventArgs e)
{
_ = DecryptWordFiles();
}