简体   繁体   中英

How to avoid Filewatcher lock file during System.IO.File.Copy

I had developed a filewatcher program to monitor a folder, if there are any changed of the file, it will copy the file to another folder.

But I found that there will be error message when writing the original file (eg file being prcoess by another application...) it seems that the file locked when running [System.IO.File.Copy] copying to another folder.

Is there any solution can avoid the original file locked by the filewatcher/System.IO.File.Copy? Thanks.

The following is my code:

    private void fileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
        DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath); 

        if (lastWriteTime != lastRead)
        {


            txtLog.Text += e.ChangeType + ": " + e.FullPath + "\r\n";
            txtLog.Focus();
            txtLog.Select(txtLog.TextLength, 0);
            txtLog.ScrollToCaret();

            try
            {
                string myPath = e.FullPath;
                string myFile = e.Name;

                System.IO.FileInfo myFileInfo = new System.IO.FileInfo(myFile);

                string myAttibs = myFileInfo.Attributes.ToString();

                System.IO.File.Copy(myPath, @"D:\\Folder\\Output\\" + myFile, true);

                lastRead = lastWriteTime; 

            }
            catch (System.IO.IOException ex)
            {
                System.IO.IOException myex = ex;
            }
            catch (System.Exception ex)
            {
                System.Exception myex = ex;
            }

        }
    }

I ran into the same problem. I am not fond of my solution, as it feels hackish. But it works:

FileSystemWatcher fsWatcher = new FileSystemWatcher();
fsWatcher.Created += new FileSystemEventHandler( fsWatcher_Created );

private void fsWatcher_Created( object sender, FileSystemEventArgs e )
{
    RaiseFileFoundEvent( e.FullPath );
    while ( !TestOpen( e.FullPath ) ) ;
    RaiseFileCopyDoneEvent( e.FullPath );
}

private bool TestOpen( string filename )
{
    try
    {
        FileStream fs = new FileStream( filename, FileMode.Open, 
            FileAccess.Write, FileShare.None );
        fs.Close();
        return true;
    }
    catch ( Exception )
    {
        return false;
    }
}

private void RaiseFileFoundEvent( string fullPath )
{
    // a file is found, but the copy is not guaranteed to be finished yet.
}

private void RaiseFileCopyDoneEvent( string fullPath )
{
    // the file is found, and we know the copy is done.
}

There's not a good way to solve this problem. How should the program behave if you're in the middle of copying the file to a new location when another application wants to write to it?

If you're willing to copy a corrupted file (that was written-to while you were copying), you'll have to write your own Copy method that uses FileShare.ReadWrite .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM