Load(string filename, string password)
デシリアライズされた DataSet を返す静的メソッドを持つ DataSet というシリアライズ可能なクラスがあります。
ここにあります:
public static DataSet Load(string filename, string password)
{
if (!File.Exists(filename))
throw new FileNotFoundException("File not found.", filename);
DataSet ds;
ICryptoTransform ct = Encryption.getDecryptor(password, salt, iv);
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
using (CryptoStream cs = new CryptoStream(fs, ct, CryptoStreamMode.Read))
{
using (GZipStream zs = new GZipStream(cs, CompressionMode.Decompress))
{
try
{
ds = (DataSet)new BinaryFormatter().Deserialize(zs);
return ds;
}
catch
{
throw new ApplicationException("This password cannot be used to decrypt this file. Either the password is incorrect or the file is corrupt");
}
finally
{
zs.Close();
}
}
}
}
}
そして、私はそれを次のように呼んでいます:
try
{
dataSet = DataSet.Load(ofd.FileName, ep.Password);
}
catch (ApplicationException ae)
{
MessageBox.Show("Error:\r\n" + ae.Message, "Authorisation Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
}
正しいパスワードを使用すると、正常に動作します。間違ったパスワードでテストしています。予想される結果は、「このパスワードを使用してこのファイルを復号化することはできません [...]」というメッセージ ボックスがポップアップ表示されることです。代わりに、キャッチされていない例外ウィンドウが表示されます。
VS でデバッグしている場合、キャッチされていない CryptographicException が発生したことがわかります。私は元々、CryptographicException 用と SerializationException 用の 2 つのキャッチを含む try/catch を持っていました。それはうまくいきませんでした。例外をキャッチするように置き換えました。最後に、私はすべてを捕まえました。
理由はわかりませんが、何らかの理由でこれをキャッチできないようですか?答えは非常に明白だと確信していますが、私にはそれが見えません。
StackoverflowException など、いくつかの例外がキャッチできないことはわかっています。CryptographicException はキャッチ不能ではないと思います。