Crown Clit

Crown Clit

  • NA
  • 14
  • 8.4k

The process cannot access the file because it is

Nov 14 2015 8:15 AM

Could you please help me out? I know it's my own process (encrypt) but have no idea how to solve it and delete the file. it throws error when hits file.delete Here is my code and error.

//Encrypt file and save in the download directory
Encrypt(Server.MapPath("~/SecPages/Documents/DocTmp/" + randomFileName + PostfileExt), Server.MapPath("~/SecPages/Documents/Downloads/" + PostfileName + PostfileExt));

//Delete file
File.Delete(Server.MapPath("~/SecPages/Documents/DocTmp/" + randomFileName + PostfileExt));

 

private void Encrypt(string inputFilePath, string outputfilePath)
        {
            string EncryptionKey = "MAKV2SPBNI99212";
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (FileStream fs = new FileStream(outputfilePath, FileMode.Create))
                {
                    using (CryptoStream cs = new CryptoStream(fs, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        FileStream fsIn = new FileStream(inputFilePath, FileMode.Open);
                        int data;
                        while ((data = fsIn.ReadByte()) != -1)
                        {
                            cs.WriteByte((byte)data);
                        }
                    }

                }
            }

        }


Error

The process cannot access the file 'K:\87.pdf' because it is being used by another process.  



















{



string EncryptionKey = "MAKV2SPBNI99212";


using (Aes encryptor = Aes.Create())






{



Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });




encryptor.Key = pdb.GetBytes(32);


encryptor.IV = pdb.GetBytes(16);



using (FileStream fs = new FileStream(outputfilePath, FileMode.Create))




{



using (CryptoStream cs = new CryptoStream(fs, encryptor.CreateEncryptor(), CryptoStreamMode.Write))




{



FileStream fsIn = new FileStream(inputFilePath, FileMode.Open);


int data;


while ((data = fsIn.ReadByte()) != -1)




{



cs.WriteByte((byte)data);




}


}


}


}


}


 


                           








Answers (1)