Windows 7 上的 Directory.Move 期间出现 IOException

发布于 2024-10-31 02:37:34 字数 2603 浏览 1 评论 0原文

我继承了一个最近部署到 Windows 7 工作站的 C# 应用程序。在此之前,它已经在许多 Windows XP 工作站上运行,没有遇到以下问题。

有问题的代码部分尝试使用线程中的循环来移动目录。在 Windows 7 机器上,IOException 被捕获。根据 MSDN (http://msdn.microsoft.com /en-us/library/system.io.directory.move.aspx) IOException 可能由 3 个条件引起。我想知道循环是否可能尝试多次移动目录,这可能会导致“目的地已存在”情况。

症状是反复显示警告消息框,但移动最终会成功。根据我对代码的解释,这应该只在 60 秒(300 * 200 毫秒)后发生,但它似乎几乎立即发生。

由于我对 C# 的经验非常少,而我对线程的经验更少,所以我在这里不知所措!我想知道相关代码是否有任何明显的错误。

相关代码部分如下。

    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'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 repeatedly, 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;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇色安年 2024-11-07 02:37:34

我首先将异常消息输出到消息框中,看看它是否可以通过执行以下操作来解释抛出异常的确切原因:

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

一旦您知道原因,您就可以查看如何防止它

此外,是什么>批量?看起来它可能试图将其移动到同一位置,这就是我在不了解有关 Batch 的更多信息的情况下的样子

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文