在 .NET 中重命名(移动)文件系统分支的最佳方法是什么?
我想通过应用字符串替换操作来递归地重命名文件和文件夹。
例如,文件和文件夹中的“shark”一词应替换为“orca”一词。
C:\Program Files\Shark Tools\Wire Shark\Sharky 10\Shark.exe
应移动到:
C:\Program Files\Orca Tools\Wire Orca\Orcay 10\Orca。当然
,同样的操作也应该应用于每个文件夹级别中的每个子对象。
我正在尝试使用 System.IO.FileInfo 和 System.IO.DirectoryInfo 类的一些成员,但没有找到简单的方法。
fi.MoveTo(fi.FullName.Replace("shark", "orca"));
没用。
我希望有某种“天才”的方式来执行这种操作。
I would like to rename files and folders recursively by applying a string replacement operation.
E.g. The word "shark" in files and folders should be replaced by the word "orca".
C:\Program Files\Shark Tools\Wire Shark\Sharky 10\Shark.exe
should be moved to:
C:\Program Files\Orca Tools\Wire Orca\Orcay 10\Orca.exe
The same operation should be of course applied to each child object in each folder level as well.
I was experimenting with some of the members of the System.IO.FileInfo
and System.IO.DirectoryInfo
classes but didn't find an easy way to do it.
fi.MoveTo(fi.FullName.Replace("shark", "orca"));
Doesn't do the trick.
I was hoping there is some kind of "genius" way to perform this kind of operation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
填写你自己的完整路径
Fill in with your own full paths
所以你会使用递归。 下面是一个 powershell 示例,应该很容易转换为 C#:
So you would use recursion. Here is a powershell example that should be easy to convert to C#: