Verify that the file was copied

0

I have the following situation.

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

How do I check if this file has already been pasted to be moved afterwards.

I already have the move method ready, I just need to check if it has already finished copying in the first directory.

    
asked by anonymous 15.04.2016 / 18:16

1 answer

1

In response to @dsfgsho for a question similar to yours:

When a file is being used it becomes unavailable, so you can check its availability and wait until it is 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;
    }
    
15.04.2016 / 18:29