无法在C#中重命名目录,但需要手动重命名

时间:2013-04-29 13:47:21

标签: c# directory rename move

我正在使用Directory.Move(oldDir, newDir)重命名目录。我不时地得到一个IOException“访问路径”oldDir“被拒绝”。但是,如果我右键单击资源管理器中的目录,我可以重命名它,没有任何问题。怎么样?我怎样才能让它发挥作用?

修改

程序仍在运行,我得到异常,并且可以在断点处暂停光标时手动重命名。我还尝试在Directory.Move设置断点,成功重命名了资源管理器中的目录(然后再重新命名),跨过Directory.Move并再次进入catch (IOException)。所以我不明白为什么我的程序应该锁定目录。必须有别的东西。

有什么想法吗?

编辑2

这是我的代码

public bool Copy()
{
    string destPathRelease = ThisUser.DestPath + "\\Release";

    if (Directory.Exists(destPathRelease))
    {
        try
        {
            string newPath = ThisUser.DestPath + '\\' + (string.IsNullOrEmpty(currBuildLabel) ? ("Release" + '_' + DateTime.Now.ToString("yyyyMMdd_HHmmss")) : currBranchName) + '.' + currBuildLabel;
            Directory.Move(destPathRelease, newPath);

            catch (IOException)
            {
               // Breakpoint
            }
        }
    }
}

如您所见,我刚刚输入了该方法。我以前从未触及过程序中的目录。还有其他方法可以重命名目录吗?

4 个答案:

答案 0 :(得分:3)

如果没有看到更多代码我会说您的应用程序正在锁定目录中的文件,您可以使用Process explorer

查看正在访问目录的内容

从简介到流程资源管理器:

有没有想过哪个程序有特定的文件或目录打开?现在你可以找到。 Process Explorer显示有关已打开或加载哪些句柄和DLL进程的信息。

也许值得确保没有别的东西是从/到该目录复制文件 - 例如Dropbox的。我最近遇到了一个问题,即visual studio会因文件锁定而停止调试 - 最后它会在暂时锁定文件的驱动器上编制索引。进程资源管理器只是部分帮助,因为它显示'system'具有文件锁定而不是另一个应用程序。

答案 1 :(得分:0)

您需要检查运行.net应用程序的用户。它没有权限执行重命名。

这是:

  • 运行Web应用程序的应用程序池的用户
  • 已记录的console / winforms应用程序的应用程序
  • 已配置的服务或计划任务用户

答案 2 :(得分:0)

如果目标目录的父目录不存在,则Directory.Move操作失败。我一直在试图找出与此大致相似的东西。

答案 3 :(得分:0)

这是使用跨平台在C#.NET Core中重命名目录的最安全方法。

    /// <summary>
    /// Renames a folder name
    /// </summary>
    /// <param name="directory">The full directory of the folder</param>
    /// <param name="newFolderName">New name of the folder</param>
    /// <returns>Returns true if rename is successfull</returns>
    public static bool RenameFolder(string directory, string newFolderName)
    {
        try
        {
            if (string.IsNullOrWhiteSpace(directory) ||
                string.IsNullOrWhiteSpace(newFolderName))
            {
                return false;
            }


            var oldDirectory = new DirectoryInfo(directory);

            if (!oldDirectory.Exists)
            {
                return false;
            }

            if (string.Equals(oldDirectory.Name, newFolderName, StringComparison.OrdinalIgnoreCase))
            {
                //new folder name is the same with the old one.
                return false;
            }

            string newDirectory;

            if (oldDirectory.Parent == null)
            {
                //root directory
                newDirectory = Path.Combine(directory, newFolderName);
            }
            else
            {
                newDirectory = Path.Combine(oldDirectory.Parent.FullName, newFolderName);
            }

            if (Directory.Exists(newDirectory))
            {
                //target directory already exists
                return false;
            }

            oldDirectory.MoveTo(newDirectory);

            return true;
        }
        catch
        {
            //ignored
            return false;
        }
    }
相关问题