从文件夹中解密 pgp 文件并移动它 - C#

decrypt pgp files from a folder and moving it - c#

我正在尝试从某个位置解密 .pgp 文件,然后将这些文件移动到另一个位置。我调查了这个 article 并相应地编写了代码。在我的代码中,我正在开发一个应用程序,它将每 100 秒检查一次某个位置,如果有文件,它将解密并移动。但是我收到了这个异常 The process cannot access the file 'c:\file.pgp' because it is being used by another process.

这是我从那篇文章中复制的 class 代码。

private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        //Do the stuff you want to be done every hour;
        string sourcePath = @"files location";
        string archivePath = @"move original file after decrypting location";
        string targetPath = @"decrypted file location";
        string pubkeyPath = @"public key location\PGPPublicKey.txt";
        string privkeyPath = @"private key location\PGPPrivateKey.txt";

        string fileName = "";
        string destFile = "";

        if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);

            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
            {
                PGPDecrypt test = new PGPDecrypt(s,
                                         privkeyPath,
                                         "password",
                                         targetPath + "decrypted.txt",
                                         pubkeyPath);
                FileStream fs = File.Open(s, FileMode.Open);
                test.decrypt(fs, targetPath + "decrypted.txt");

                // Use static Path methods to extract only the file name from the path.
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(archivePath, fileName);
                System.IO.File.Move(s, archivePath);
            }
        }        
    }

你在哪里得到错误。如果您在移动时遇到错误,可能是因为您的文件流没有关闭。解密后和移动前用 fs.Close();

关闭文件流

我认为您遇到的问题是由于文件未关闭引起的,当您使用 foreach 循环进行循环时,第一次迭代可能会起作用。不过下一次,因为一直没关,所以还在用

尝试添加

fs.Close();

foreach 循环结束时

这是我完成的并且正在运行

//Decrypt
using DidiSoft.Pgp;
 PGPLib pgp = new PGPLib();

            string inputFileLocation = file Location; 
            string privateKeyLocation = @"I posted my private at this location";
            string privateKeyPassword = "Decryption Password";
            string outputFile = @"Output Location";

            // decrypt and obtain the original file name
            // of the decrypted file
            string originalFileName =
              pgp.DecryptFile(inputFileLocation,
                          privateKeyLocation,
                          privateKeyPassword,
                          outputFile);
//Move decrypted file to archive
string path = Decrypted file Location;
            string path2 = @"Archive file location" + Path.GetFileName(file); ;
            try
            {
                if (!File.Exists(path))
                {
                    // This statement ensures that the file is created,
                    // but the handle is not kept.
                    using (FileStream fs = File.Create(path)) { }
                }

                // Ensure that the target does not exist.
                if (File.Exists(path2))
                    File.Delete(path2);

                // Move the file.
                File.Move(path, path2);

            }
            catch (Exception e)
            {

            }