DirectoryInfo.MoveTo-“源和目标路径必须具有相同的根.移动无法跨卷进行." [英] DirectoryInfo.MoveTo - "Source and destination path must have identical roots. Move will not work across volumes."

查看:2255
本文介绍了DirectoryInfo.MoveTo-“源和目标路径必须具有相同的根.移动无法跨卷进行."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚这里发生了什么.我有一个例程,它查看目录中的所有目录,并从目录名称的第一部分中删除所有非数字值.由于某种原因,当执行MoveTo时,我得到源路径和目标路径必须具有相同的根.移动将无法跨卷工作."但是我只提供新名称作为参数.因此目录可能是"007A Raby",而传递给MoveTo的新名称将是"007 Raby".有人对我在做什么错有任何想法吗?

I'm trying to figure out what is going on here. I have a routine that looks at all the directories in a directory and removes any non numeric values from the first part of a directory name. For some reason when it goes to do the MoveTo I'm getting the "Source and destination path must have identical roots. Move will not work across volumes." But I'm only providing the new name as the parameter. So the directory might be "007A Raby" and the new name passed into MoveTo would be "007 Raby". Anybody have any thoughts on what I'm doing wrong?

    private void RenameSubs(string directory)
    {
        try
        {
            if (Directory.Exists(directory))
            {
                var parentDI = new DirectoryInfo(directory);

                foreach (var di in parentDI.GetDirectories())
                {
                    var spaceLocation = di.Name.IndexOf(' ');
                    var changed = false;

                    if (spaceLocation > 0)
                    {
                        var oldName = di.Name;
                        var subPartA = di.Name.Substring(0, spaceLocation);
                        var subPartB = di.Name.Substring(spaceLocation, di.Name.Length - spaceLocation);

                        for (int i = subPartA.Length - 1; i > 0; i--)
                        {
                            if (subPartA[i] < '0' || subPartA[i] > '9')
                            {
                                subPartA = subPartA.Substring(0, i);
                                changed = true;
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (changed)
                        {
                            if (!Directory.Exists(Path.Combine(directory, subPartA + subPartB)))
                            {
                                var newName = subPartA + subPartB;
                                di.MoveTo(newName);
                                txtOutput.Text += "Renamed " + oldName + " to " + di.Name + "\r\n";
                            }
                            else
                            {
                                txtOutput.Text += "Error " + oldName + " already exists " + "\r\n";
                            }
                        }
                        else
                        {
                            txtOutput.Text += "Ignored " + di.Name + "\r\n";
                        }
                    }
                }
            }   
        }
        catch (System.Exception excpt)
        {
            txtOutput.Text += "Error " + excpt.Message + "\r\n";
            Console.WriteLine(excpt.Message);
        }
    }

推荐答案

好,经过反复试验,我弄清楚了如何解决它.当相对路径"传递到DirectoryInfo.MoveTo时,它不使用父路径,而是使用应用程序路径.因此,当我说它能正常工作是因为它与应用程序位于同一驱动器上时,我错过了将文件夹重命名为应用程序文件夹的想法.为了解决这个问题,我需要传递一个绝对路径到MoveTo方法.这是在如果(已更改)"代码块中进行此工作所需的代码更改:

Ok, after trial and error I figured out how to fix it. when a "relative path" is passed into DirectoryInfo.MoveTo it doesn't use the Parent Path but the application path. So when I said it worked because it was on the same drive as the application I missed that it renamed the folders to the application folder. To fix this I needed to pass in an absolute path to the MoveTo method. Here is the code change needed inside the "if (changed)" code block to have this work:

    var newName = Path.Combine(directory, subPartA + subPartB);

    if (!Directory.Exists(newName))
    {
        di.MoveTo(newName);
        txtOutput.Text += "Renamed " + oldName + " to " + di.Name + "\r\n";
    }
    else
    {
        txtOutput.Text += "Error " + oldName + " already exists " + "\r\n";
    }

希望这对其他人有帮助.

Hope this helps somebody else.

这篇关于DirectoryInfo.MoveTo-“源和目标路径必须具有相同的根.移动无法跨卷进行."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆