【问题标题】:using fileinfo instead of string使用文件信息而不是字符串
【发布时间】:2013-10-30 09:09:09
【问题描述】:

我有以下代码:

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 公钥加密文件并使用密码和私钥对其进行解密。

请多指教!!!谢谢!

【问题讨论】:

    标签: filestream fileinfo


    【解决方案1】:

    你可以试试这样的:

    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,所以无法自己验证..

    【讨论】:

    • 现在我可以调试程序并正常运行,但是当我点击我的加密按钮时,'using (var inputStream = new FileStream(openFileDialog1.FileName, FileMode .Open))' 当我在我的程序上使用 F11 时,会发生这种情况: mscorlib.dll 中发生了“System.IO.IOException”类型的第一次机会异常附加信息:进程无法访问文件“C:\ Users\Tian Ci\Desktop\fdasfdafda.txt' 因为它正在被另一个进程使用。
    • @user2930173 您需要知道哪个进程阻止了该文件并删除该块(例如通过终止该进程)。有许多实用程序可以执行此操作,例如 - 来自 SysInternals 的句柄:technet.microsoft.com/en-us/sysinternals/bb896655
    • 我已经更新了答案,现在这个锁不应该阻止阅读
    • 尝试新答案后,单击加密按钮时没有打开文件对话框。这意味着我无法选择我的公钥来加密我的文件。 FileStream 是否打开一个对话框?真的很抱歉给您带来麻烦。我不是编程学生。我学到了非常基础的编程语言。
    • 没有文件流不会打开对话框。您需要单独打开它。基本上,您可以使用硬编码文件名(如第一个示例中的 @"C:\TCkeyPublic.txt" )测试文件流,然后找出文件对话框有什么问题,因为实际上它与原始问题无关跨度>
    【解决方案2】:

    发生文件锁定是因为来自 DidiSoft API 的 EncryptFile 方法也尝试从同一文件中获取流。可能下面的示例 sn-p 是您打算做的:

    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 与我们联系

    【讨论】:

      猜你喜欢
      • 2018-02-15
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 2014-10-21
      • 2013-09-19
      相关资源
      最近更新 更多