Check that the file has been copied

Asked

Viewed 1,398 times

0

I have the following situation.

A file is placed in a certain directory, when it is copied need to move it to another folder.

How do I check if you have finished pasting this file to be moved after.

I already have the method of moving ready, I just need to check if you have finished copying in the first directory.

  • Compare his size?

1 answer

1


Agree with the response of @dsfgsho to a question similar to yours:

When a file is being used it becomes unavailable, so you can check its availability and wait until it becomes available for use.

    void AguardaArquivo()
    {
        // Seu aqruivo
        var file  = new FileInfo("caminho/do/arquivo");

        // Enquanto o arquivo não está acessível, deve estar sendo copiado
        while (IsFileLocked(file)) { }

        // A partir daqui o arquivo está disponível

    }

    /// <summary>
    /// Code by ChrisW -> https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use
    /// </summary>
    protected virtual bool IsFileLocked(FileInfo file)
    {
        FileStream stream = null;

        try
        {
            stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
        }
        catch (IOException)
        {
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }

        //file is not locked
        return false;
    }
  • Thank you very much, it worked this way

Browser other questions tagged

You are not signed in. Login or sign up in order to post.