Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在System.IO.file.Copy期间避免Filewatcher锁定文件_C#_Asp.net_File_.net - Fatal编程技术网

C# 如何在System.IO.file.Copy期间避免Filewatcher锁定文件

C# 如何在System.IO.file.Copy期间避免Filewatcher锁定文件,c#,asp.net,file,.net,C#,Asp.net,File,.net,我开发了一个filewatcher程序来监视一个文件夹,如果文件有任何更改,它会将文件复制到另一个文件夹 但我发现在写入原始文件时会出现错误消息(例如,文件被另一个应用程序复制…),在运行[System.IO.file.Copy]复制到另一个文件夹时,文件似乎被锁定 是否有任何解决方案可以避免filewatcher/System.IO.file.Copy锁定原始文件?谢谢 以下是我的代码: private void fileWatcher_Changed(object sender, S

我开发了一个filewatcher程序来监视一个文件夹,如果文件有任何更改,它会将文件复制到另一个文件夹

但我发现在写入原始文件时会出现错误消息(例如,文件被另一个应用程序复制…),在运行[System.IO.file.Copy]复制到另一个文件夹时,文件似乎被锁定

是否有任何解决方案可以避免filewatcher/System.IO.file.Copy锁定原始文件?谢谢

以下是我的代码:

    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;
            }

        }
    }

没有解决这个问题的好办法。如果在另一个应用程序想写入文件时将程序复制到一个新的位置,程序应该如何运行?
如果您想复制损坏的文件(在复制时写入的文件),则必须编写自己的复制方法,使用
FileShare.ReadWrite

来解决此问题。如果在另一个应用程序想写入文件时将程序复制到一个新的位置,程序应该如何运行?
如果要复制损坏的文件(在复制时写入的),则必须编写自己的复制方法,该方法使用
FileShare.ReadWrite

我遇到了相同的问题。我不喜欢我的解决方案,因为它让人感觉很不舒服。但它是有效的:

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.
}

我遇到了同样的问题。我不喜欢我的解决方案,因为它让人感觉很不舒服。但它是有效的:

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.
}

我认为抓住例外是你能做的最好的事情。您真的想在副本中写入文件吗?问问自己,为什么要将这个文件从一个文件夹复制到另一个文件夹,看看是否可以重新访问您的设计。我认为捕获异常是您能做的最好的事情。Yikes。您真的想在副本中写入文件吗?问问自己为什么要将此文件从一个文件夹复制到另一个文件夹,看看是否可以重新访问您的设计。我也不喜欢这个解决方案,但至少要放松while循环。(比我刚才删除的注释更好)在我的生产代码中,它会检查线程是否已中止。为了清楚起见,我把它删掉了。我也不喜欢这个解决方案,但至少放松了while循环。(比我刚才删除的注释更好)在我的生产代码中,它检查线程是否已中止。为了清楚起见,我把它删掉了。