开发者

IOException during Directory.Move on Windows 7

I've inherited a C# app which has recently been deployed to Windows 7 workstations. Before this it has been running on many Windows XP workstations without experiencing the issue below.

The section of code in question tries to move a directory, using a loop in a thread. On the Windows 7 machines an IOException is caught. According to MSDN (http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx) IOException can be caused by 3 conditions. I am wondering if the loop may be trying to move the directory more than once, which might cause the "destination already exists" condition.

The symptom is that the warning MessageBox is shown r开发者_如何学Cepeatedly, but the move will eventually succeed. From my interpretation of the code, this should only happen after 60 seconds (300 * 200ms), but it seems to occur almost instantly.

As my experience with C# is very small, and my experience with threads even smaller, I'm at a loss here! I'm wondering if there is anything obviously wrong with the code in question.

The relevant section of code is below.

    public static string archiveBatch(Batch myBatch, string from)
    {
        string to = "";

        to = FileSystemManager.getArchivePath(myBatch, System.IO.Path.GetFileName(from));

        threadedMove tm = new threadedMove(from ,to);

        Thread t = new Thread(new ThreadStart(tm.run));
        t.Priority = ThreadPriority.Highest;
        t.Start();

        return to;
    }

    private class threadedMove
    {
        string archivePath;
        string fromPath;

        public threadedMove(string from, string to)
        {
            archivePath = to;
            fromPath = from;
        }

        public void run()
        {
            int errorCounter = 0;

            while (true)
            {
                errorCounter++;
                if (TryToMove(fromPath, archivePath)) { break; }
                Thread.Sleep(200);
                if (errorCounter > 300)
                {
                    throw (new Exception("Warning: could not archive file from "+fromPath+" to "+archivePath));
                }
            }
        }
    }

    private static bool TryToMove(string source, string destination)
    {
        try
       {
             //check if path is file or folder
            if (System.IO.File.Exists(source))
            {
                //it is a file
                if (!System.IO.File.Exists(destination))
                {
                    System.IO.File.Move(source, destination);
                }
            }
            else
            {
                //it is a directory
                if (!System.IO.Directory.Exists(destination))
                {
                    System.IO.Directory.Move(source, destination);
                }
            }

            return true;
        }
        catch (IOException)
        {
            MessageBox.Show("Warning: could not archive file from " + source + " to " + destination", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return false;
        }
    }


I'd start by outputting the exception message into the messagebox to see if it sheds some light on exactly why the exception is being thrown by doing this:

catch (IOException ex)
{
    MessageBox.Show("Warning: could not archive file from " + source + " to " + destination + ". Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    return false;
}

Once you know the reason you can then look at how to prevent it

Also what is Batch? It looks like it could be trying to move it to the same location, by that's just how it looks to me without knowing more about Batch

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜