开发者

System.IO.File.Copy() produces System.IO.IOException: The specified network name is no longer available

I have a windows service (C# .Net 3.5) that grabs data from a network share and does a copy to the host of the service.

The size of the data copied ranges from 50KB to 750MB, and the number of files copied varies. In perhaps 20% of the copies I am getting System.IO.IOException: The specified network name is no longer available.

My google-fu is failing to turn up an answer as to what might cause this during a File.Copy. Has anyone seen/solved this before?

Here is the recursive method that does the copy. The exception occurs on line File.Copy(fromFile, toFile, overwrite);

private static int RecursiveCopyDirectory(string from, string to, bool merge, bool overwrite, int depth)
    {
        depth++;

        if (!from.EndsWith(Path.DirectorySeparatorChar.ToString()))
        {
            to += Path.DirectorySeparatorChar;
        }
        if (!to.EndsWith(Path.DirectorySeparatorChar.开发者_如何学JAVAToString()))
        {
            to += Path.DirectorySeparatorChar;
        }

        System.Diagnostics.Debug.WriteLine(string.Format("RecursiveDirectoryCopy( {0}, {1}, {2} )", from, to, merge));
        if (Directory.Exists(to))
        {
            if (!merge)
            {
                return (int)EventEnum.FileSystemError_DirectoryAlreadyExists;
            }
        }
        else
        {
            Directory.CreateDirectory(to);
        }

        string[] directories = Directory.GetDirectories(from);

        foreach (string fromDirectory in directories)
        {
            string [] fromDirectoryComponents = fromDirectory.Split(Path.DirectorySeparatorChar);
            string toDirectory = to + fromDirectoryComponents[fromDirectoryComponents.Length - 1];
            RecursiveCopyDirectory(fromDirectory, toDirectory, merge, overwrite, depth);
        }

        string[] files = Directory.GetFiles(from);

        foreach (string fromFile in files)
        {
            string fileName = Path.GetFileName(fromFile);
            //System.Diagnostics.Debug.WriteLine(string.Format("Name: {0}", to + fileName));    
            string toFile = to + fileName;

            File.Copy(fromFile, toFile, overwrite);
        }

        return (int)EventEnum.GeneralSuccess;
    }


File.Copy() opens up underline streams. You might have lost connection while File.Copy() is in progress. So, it can't flush and close the stream.

One possibility to recover from this, is to use the FileStream class and call Win32 API CloseHandle when such exception occurs, doing so will release the OS file handle so you can re-open the file.

[ DllImport("Kernel32") ]
public static extern bool CloseHandle(IntPtr handle);

FileStream fs;
try {
...
}

catch(IOException)
{
// If resource no longer available, or unable to write to.....
if(...)
CloseHandle(fs.Handle);
}

Also, MSDN recommends not to rely on overwrite. Try deleting existing file and creating new one when copying them.

File.Copy(..., ..., TRUE) does not work properly.

Be very careful with this method, as the Overwrite = True does NOT work properly.

I had an existing destination file that had some information inside it that was somehow preserved and carried over to the source file that was supposed to copy over it.  This should be impossible, but I confirmed it for myself.


The error seems to indicate that the network connection is lost partway through and probably isn't to do with the code at all. If the same folder copy succeeds sometimes and fails other times then this would back up that it's not the code to blame and must be a resource access issue.


It turns out that the customer that was using this software was running two instances of it simultaneously, against the same data set. Once the redundant instance was stopped it resolved the error. Thanks everyone who answered.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜