File gets locked when overwriting

1.3k views Asked by At

Title explains a small part so let me explain 2 scenarios. Scenario 1 is raising errors, scenario 2 works like a charm.

Scenario 1:

I checkout a document with the method below, when the document is saved to a location where already is a file with that name it gets overwritten, But surprisingly it also locks the file for some reason:

public bool SaveDocument(int bestandsId, string fileName, string path)
{
    //Initialize the Sql Query
    var sql = "SELECT DATA FROM Documenten WHERE BESTAND_ID = " + bestandsId;
    //Initialize SqlConnection
    var connection = new SqlConnection(Instellingen.Instance.DmsConnectionString);
    //Initialize SqlCommand
    var command = new SqlCommand(sql, connection);

    try
    {
        //Open Connection
        connection.Open();
        //Fill 'data' from command.ExecuteScalar()
        var data = (byte[]) command.ExecuteScalar();
        //Write 'data' to file.
        File.WriteAllBytes(path + @"\" + fileName, data);
        //Return true if no exceptions are raised.
        return true;
    }
    catch (Exception ex)
    {
        //Initialize Dms Exception
        var dmsEx = new DmsException(ex);
        //Write Dms Exception to Log File.
        DmsException.WriteErrorsToLog(dmsEx);
        //Return false, because something went wrong...
        return false;
    }
    finally
    {
        //Close Sql Connection
        connection.Close();
    }
}

The method runs smoothly. No problems occur. But when I check in the document with the method below, I get this exception:

Exception

Scenario 2:

When I use the SaveDocument method to save the document to a location where there isn't a file with the same name, the file is newly created and is ready to be edited or what ever you want to do with it.

Using scenario 2 is working perfect. The document is ready to be checked in again without receiving an error as shown in the picture above.

Request for code by: @CodeCaster

---------------------------------BEGIN EDIT---------------------------------

    public static bool InsertDocument(Document document)
    {
        try
        {
            //Exception is thrown when Initializing the FileStream
            var fileStream = new FileStream(document.Fileinfo.FullName, FileMode.Open, FileAccess.Read); 

            var binaryReader = new BinaryReader(fileStream);
            var totalNumberOfBytes = new FileInfo(document.Fileinfo.FullName).Length;
            var data = binaryReader.ReadBytes((Int32) totalNumberOfBytes);

            fileStream.Close();
            fileStream.Dispose();
            binaryReader.Close();
            binaryReader.Dispose();

            var pdftext = string.Empty;
            try
            {
                if (document.DocumentType == ".pdf")
                {
                    var reader = new PdfReader(document.Fileinfo.FullName);
                    var text = string.Empty;
                    for (var page = 1; page <= reader.NumberOfPages; page++)
                    {
                        text += PdfTextExtractor.GetTextFromPage(reader, page);
                    }
                    reader.Close();
                    pdftext = text;
                }
            }
            catch (Exception ex)
            {
                var dmsEx = new DmsException(ex);
                DmsException.WriteErrorsToLog(dmsEx);
            }


            return InsertIntoDatabase(document.BestandsNaam, document.Eigenaar, document.Omschrijving,
                                      document.DatumToevoeg.ToString(), document.DatumIncheck.ToString(),
                                      document.DatumUitcheck.ToString(), document.UitgechecktDoor,
                                      document.DocumentType, data, pdftext, document.Versie, document.Medewerker,
                                      document.DossierNummer, document.PersonalFolderId.ToString(),
                                      document.DossierFolderId, -1, document.DocumentProgres,
                                      document.OriBestandId.ToString(), 0);
        }
        catch (Exception ex)
        {
            var dmsEx = new DmsException("Fout bij inlezen voor toevoeging van nieuw document",
                                         "Klasse Document (InsertDocument)", ex);
            ExceptionLogger.LogError(dmsEx);

            return false;
        }
    }

---------------------------------END EDIT---------------------------------

My questions:

  1. What is the cause for the file being locked when it gets overwritten?
  2. How can I prevent this from happening?
  3. Is there some sort of function or parameter that I can set so it doesn't get locked?

Using a tool called "Unlocker" I managed to see what program is locking the file, and YES -> DMS.exe is my application.......: enter image description here

1

There are 1 answers

1
briba On
using(var stream = File.Create(newPath)){}
File.WriteAllBytes(newPath, item.File);

With StreamWriter

using (FileStream fs = File.Create(newPath))
{
    fs.Write(item.File, 0, item.File.Length);
}

Or:

File.WriteAllBytes(newPath, item.File);

Reference: "The process cannot access the file because it is being used by another process" with Images