使用fileinfo而不是string

时间:2013-10-30 09:09:10

标签: filestream fileinfo

我的代码如下:

private void encryptFileButton_Click(object sender, EventArgs e)
        {
            try
            {
                bool asciiArmor = false;
                bool withIntegrityCheck = false;
                pgp.EncryptFile(@attachmentTextBox.Text,
                         @"C:\TCkeyPublic.txt",
                         @"C:\OUTPUT.pgp",
                         asciiArmor,
                         withIntegrityCheck);
                MessageBox.Show("File Encrypted Successfully!");
            }
            catch
            {
                MessageBox.Show("File Encryption Fail!");
            }            
        }

我想将@"C:\TCkeyPublic.txt"更改为new FileInfo(openFileDialog1.FileName)之类的内容。因此,当用户具有不同的文件名或文件路径时,用户不必总是更改代码。

但是当我尝试这样做时,代码下面有这条红色的zizag行,当我把鼠标放在上面时,它说它无法将System.IO.FileInfo转换为System.IO.Stream。

                        try
                        {
                            bool asciiArmor = false;
                            bool withIntegrityCheck = false;
                            pgp.EncryptFile(@attachmentTextBox.Text,
                                     new FileInfo(openFileDialog1.FileName),
                                     @"C:\OUTPUT.pgp",
                                     asciiArmor,
                                     withIntegrityCheck);
                            MessageBox.Show("File Encrypted Successfully!");
                        }
                        catch
                        {
                            MessageBox.Show("File Encryption Fail!");
                        } 

我正在使用didisoft pgp(BouncyCastle.CryptoExt.dll和DidiSoft.Pgp.dll)构建我的项目,使用PGP使用PGP公钥加密文件,并使用密码和私钥对其进行解密。

请咨询!!!谢谢!

2 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

using System.IO;
...
using (var inputStream = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var outputStream = new FileStream(@"C:\OUTPUT.pgp", FileMode.OpenOrCreate))
{
   pgp.EncryptFile(@attachmentTextBox.Text, inputStream, outputStream, ..
}

没有你的didisoft所以不能自己验证..

答案 1 :(得分:0)

发生文件锁定是因为DidiSoft API中的EncryptFile方法也尝试从同一文件中获取流。可能你想要做的示例片段如下:

bool asciiArmor = false;
bool withIntegrityCheck = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK) 
{
  pgp.EncryptFile(@attachmentTextBox.Text,
                  openFileDialog1.FileName,
                  @"C:\OUTPUT.pgp",
                  asciiArmor,
                  withIntegrityCheck);
}

如果您更喜欢使用流,可以检查EncryptStream方法,如图所示 http://www.didisoft.com/net-openpgp/examples/encrypt-file/#EncryptStream

顺便提一下,欢迎您直接通过support@didisoft.com与我们联系

相关问题