无法通过 C# 控制台应用程序中的 System.IO.File 删除某些文件

Unable to delete some files via System.IO.File in C# console app

我正在加密一些文本文件。它工作正常,文件被加密,但有时我在尝试删除原始未加密文件时遇到此错误:

System.IO.IOException: The process cannot access the file 'MyFile.TXT' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path) at FileEncryption.Program.DeleteFile(String sInputFilename) in FileEncryption\Program.cs:line 159

这似乎发生在大型文本文件上。 (50MB+)但并非总是如此。

知道我做错了什么吗?

处理txt文件文件夹的方法:

private static void BeginFileProcessing(string sSecretKey_)
{
    DirectoryInfo di = new DirectoryInfo(_sourcePath);
    FileInfo[] files = di.GetFiles(_fileType);

    try
    {
        foreach (FileInfo file in files)
        {
            string thisFileExt = Path.GetExtension(file.Name);
            string thisFileName = Path.GetFileNameWithoutExtension(file.Name);
            string encFileName = String.Format("{0}-enc{1}", thisFileName, thisFileExt);

            if (_TestingOnly)
            {
                Console.Write("Source: " + file.Name + " " + 
                    " Encrypted File: " + encFileName + "\n");
            }

            EncryptFile(file.FullName, _targetPath + encFileName, sSecretKey_);

            if (_DeleteOriginal)
            {
                Console.WriteLine("Deleteing file: " + file.FullName);
                DeleteFile(file.FullName);
            }
        }
    }
    catch (Exception ex)
    {
        LogWriter(string.Format("\nError Decrypting file: {0}", ex), true);
    }
}

加密文件的方法

private static void EncryptFile(string sInputFilename, 
    string sOutputFilename, string sKey)
{
    FileStream fsInput = 
        new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);

    FileStream fsEncrypted = 
        new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);

    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();

    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    ICryptoTransform desencrypt = DES.CreateEncryptor();

    CryptoStream cryptostream = 
        new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);

    try
    {
        byte[] bytearrayinput = System.IO.File.ReadAllBytes(sInputFilename);
        fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
        cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
        cryptostream.Close();
        fsInput.Close();
        fsEncrypted.Close();
    }
    catch (Exception ex)
    {
        string error = "";

        foreach (DictionaryEntry pair in ex.Data)
        {
            error += pair.Key + " = " + pair.Value + "\n";
            Console.WriteLine(error);
        }

        LogWriter(error, true);
    }
} 

删除文件的方法

private static void DeleteFile(string sInputFilename)
{
    try
    {
        if (_TestingOnly)
        {
            Console.WriteLine("TESTING ONLY! File: " + sInputFilename + " would have been deleted.");
        }
        else
        {
            File.Delete(sInputFilename);
        }
    }
    catch (Exception ex)
    {
        Console.Write(ex.ToString());
        LogWriter(ex.ToString(), true);
    }
}

这可能是因为您的文件在调用 EncryptFile 后没有关闭。在您的原始代码中,如果您在 EncryptFile 中遇到异常,并且在调用 Close 之前发生异常,则流将保持打开状态。使用 Using 语句使这更容易,但您也可以将 Close 放在 finally 块中,如果它们不为空则关闭流。

这是一个使用 Using 的例子:

private static void EncryptFile(string sInputFilename, 
    string sOutputFilename, string sKey)
{
    using(FileStream fsInput =  new FileStream(sInputFilename, FileMode.Open, FileAccess.Read),
            FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write))
    {

        DESCryptoServiceProvider DES = new DESCryptoServiceProvider();

        DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        ICryptoTransform desencrypt = DES.CreateEncryptor();

        using(CryptoStream cryptostream = 
            new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write))
        {
            try
            {
                byte[] bytearrayinput = System.IO.File.ReadAllBytes(sInputFilename);
                fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
                cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
            }
            catch (Exception ex)
            {
                string error = "";

                foreach (DictionaryEntry pair in ex.Data)
                {
                    error += pair.Key + " = " + pair.Value + "\n";
                    Console.WriteLine(error);
                }

                LogWriter(error, true);
            }
        }
    }
}

编辑

我建议的代码将解决文件流保持打开状态的问题。但是,问题的根源在于如果文件很大,系统会在读取文件时抛出 OutOfMemoryException。原始代码会读取所有字节,然后再次将字节读取到同一个缓冲区中,这是对内存和时间的浪费。以下是更正后的版本:

private static void EncryptFile(string sInputFilename, 
    string sOutputFilename, string sKey)
{
    using(FileStream fsInput =  new FileStream(sInputFilename, FileMode.Open, FileAccess.Read),
            fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write))
    {

        DESCryptoServiceProvider DES = new DESCryptoServiceProvider();

        DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        ICryptoTransform desencrypt = DES.CreateEncryptor();

        using(CryptoStream cryptostream = 
            new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write))
        {
            byte[] buffer = new byte[2048];
            int readCount = 0;

            try
            {
                while ((readCount = fsInput.Read(buffer, 0, 2048)) > 0)
                {
                    cryptostream.Write(buffer, 0, readCount);
                }
            }
            catch (Exception ex)
            {
                string error = "";

                foreach (DictionaryEntry pair in ex.Data)
                {
                    error += pair.Key + " = " + pair.Value + "\n";
                    Console.WriteLine(error);
                }

                LogWriter(error, true);
            }
        }
    }
}

您应该考虑对所有实现 IDisposable 的对象使用 using 语句。这将确保它们在 using 块的末尾关闭和处置:

private static void EncryptFile(string sInputFilename, string sOutputFilename, 
    string sKey)
{
    using (var fsInput = new FileStream(sInputFilename, FileMode.Open, 
        FileAccess.Read))
    using (var fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, 
        FileAccess.Write))
    using (var desCryptoProvider = new DESCryptoServiceProvider())
    {
        desCryptoProvider.Key = Encoding.ASCII.GetBytes(sKey);
        desCryptoProvider.IV = Encoding.ASCII.GetBytes(sKey);

        using (var encryptor = desCryptoProvider.CreateEncryptor())
        using (var cryptoStream = new CryptoStream(fsEncrypted, encryptor, 
            CryptoStreamMode.Write))
        {
            try
            {
                var bytearrayinput = File.ReadAllBytes(sInputFilename);
                fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
                cryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length);
            }
            catch (Exception ex)
            {
                var errors = new StringBuilder();

                foreach (var pair in ex.Data)
                {
                    errors.AppendLine(string.Format("{0} = {1}", pair.Key, pair.Value));
                }

                Console.WriteLine(errors.ToString());
                LogWriter(errors.ToString(), true);
            }
        }
    }
}

这不是一个确切的答案,但可能会有所帮助;检查文件是否被锁定的简单方法:

bool IsFileAvailable(string fileName)
{
    FileStream stream = null;

    try
    {
        FileInfo fileInfo = new FileInfo(fileName);
        stream = fileInfo.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    }
    catch (IOException)
    {
        // File is not present, or locked by another process
        return false;
    }
    finally
    {
        if (stream != null)
            stream.Close();
    }

    // File is present and not locked
    return true;
}