如何在 C# 中重命名文件夹/目录?
让: 要重命名的文件夹 c:\temp\Torename 到: c:\temp\ToRename
Directory.Move 不起作用,因为文件夹(:\temp\Torename) 已存在。
我正在寻找一种不涉及创建临时文件夹的解决方案。 我有这个解决方案: 移动到临时文件夹(唯一名称),例如 c:\temp\TorenameTemp 从临时文件夹移动到新文件夹。例如 c:\temp\ToRename 问题是我的文件夹可能会变得非常大,并且移动可能需要一些时间才能执行。我喜欢 Windows 资源管理器解决方案,其中用户可以立即重命名,无论大小如何。
谢谢您的宝贵时间。
Let:
Folder to rename
c:\temp\Torename
to:
c:\temp\ToRename
Directory.Move does not work because the folder(:\temp\Torename) already exist.
I am looking for a solution that does not involve creating a temp folder.
I have this solution in place:
Move to a temp folder (unique name) eg c:\temp\TorenameTemp
Move from temp folder to new folder. eg c:\temp\ToRename
The problem is that my folder can get very big and move can take some time to execute. I like windows explorer solution in which user renames on the spot regardless of size.
thanks for yor time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
除非您将文件移动到不同的卷,否则它们不会被移动。如果目标位于同一卷上,则仅目录名称会更改。
The files won't be moved unless you are moving them to a different volume. If destination is on the same volume, only the name of directory will change.
Directory.Move
不会随目录大小缩放(除非您复制到不同的驱动器),因此调用它两次没有任何问题。Directory.Move
doesn't scale with the directory size (unless you're copying to a different drive), so there's nothing wrong with calling it twice.2020 解决方案:这是我安全重命名目录的方法。
2020 Solution: Here's my method to rename a directory safely.
这样做的方法如下:
第一个参数是当前目录,第二个参数是目录的新名称。
来源:文件系统。重命名目录方法
Here is how it could be done:
The first parameter is the current directory, the second parameter is the new name of the directory.
Source: FileSystem.RenameDirectory Method
在寻找完整的解决方案后,我将每个人的建议/担忧都包含到这个实用方法中。
您可以用简单的方式调用它:
根据我在 MSDN 文档中找到的内容,RenameDirectory 方法中提供的是所有可能的异常的全面爆发。您可以选择将它们简化为单个 try-catch 块(可能,但不推荐),或者您可能希望修改异常的处理方式,但这应该可以帮助您继续下去。我还添加了第三个可选参数,允许您根据需要抑制异常,该参数只会返回 false 而不会引发异常。它默认为 false(即不抑制异常)。
我还没有尝试每个路径来测试,但我会非常小心地重命名驱动器,例如“C:\”到“E:\”。它可能会抛出异常...但我还没有测试过。 :-)
总的来说,只需复制/粘贴这个解决方案,也许删除一些评论/研究链接,它就会起作用。
After looking around for a complete solution, I wrapped everyone's advice/concerns into this utility method.
You can call it with a simple:
Provided in the RenameDirectory method is a full-blowout of all possible exceptions, according to what I found in MSDN documentation. You may choose to simplify them down to a single try-catch block (possible, not recommended), or you may wish to modify how exceptions are handled, but this should get you going. I've also included a third, optional, parameter that allows you to suppress exceptions if you want to, which would just return a false instead of throwing an exception. It is defaulted to false (ie, doesn't suppress exceptions).
I have not tried out each path to test, but I would be VERY careful about renaming a drive, such as "C:\" to "E:\". It would probably throw an exception... but I've not tested. :-)
Overall, just copy/paste this solution and maybe remove some comments/links to research and it will work.
目录.移动目录
文件.移动文件
Directory.Move for directory
File.Move for file