文件夹和文件操作 [英] Folder and file operations
问题描述
我是 VB 新手,但我需要使用 VB 在我的项目中实现这些.
I am new to VB but I need to achieve these in my project using VB.
我在本地有一个文件 target.file
.网络服务器上有一个 target
文件夹.target
下有几百个文件夹.其中一些文件夹下有 backup
文件夹.
I have a file target.file
at local. There is a target
folder on network server. There are several hundred folders under target
. There are backup
folders under some of those folders.
我需要将 target.file
复制到 target
文件夹下的那些文件夹,但如果已经有 target.file代码> 现有.如果
target.file
存在,如果备份文件夹存在,则将文件复制并替换到备份文件夹.如果没有,请先创建备份文件夹.
I need to copy target.file
to those folders under the target
folder, but only copy/replace if there is already a target.file
existing. If the target.file
exists, copy and replace the file to the backup folder if the backup folder exists. If not, create the backup folder first.
推荐答案
您需要使用 System.IO
命名空间中的方法.查看文档,有很多有趣有用的类,特别是文件,目录 和 路径.
You need to use the methods in System.IO
namespace. Take a look at the Documentation, there is a lot of interesting and useful classes, specially File, Directory and Path.
以下是您需要的方法:
- File.Exists():检查给定路径上是否存在文件.
- File.Copy():复制给定路径上的文件.
- Directory.Exists():检查给定路径上是否存在目录.
- Directory.CreateDirectory():创建给定路径上不存在的所有目录.
但是,您无需检查文件是否存在,因为您可以在启用覆盖选项的情况下直接复制:
However, you didn't need to check if the file exists because you can copy directly with the overwrite option enabled:
File.Copy(sourceFile, destPathFile, True) 'True for overwrite
这篇关于文件夹和文件操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!