欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

VB.NET拷贝整个目录下所有子目录及文件的实例代码

程序员文章站 2024-02-29 13:01:40
复制代码 代码如下: public sub copyderictory(byval directorysrc as directoryinfo, byval di...
复制代码 代码如下:

 public sub copyderictory(byval directorysrc as directoryinfo, byval directorydes as directoryinfo)
        dim strdirectorydespath as string = directorydes.fullname & "" & directorysrc.name
        if not directory.exists(strdirectorydespath) then
            directory.createdirectory(strdirectorydespath)
        end if
        dim f, fs() as fileinfo
        fs = directorysrc.getfiles()
        for each f in fs
            file.copy(f.fullname, strdirectorydespath & "" & f.name, true)
        next
        dim dirsrc, dirs() as directoryinfo
        dirs = directorysrc.getdirectories()
       // 递归调用自身
        for each dirsrc in dirs
            dim dirdes as new directoryinfo(strdirectorydespath)
            copyderictory(dirsrc, dirdes)
        next
    end sub
    private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
        copyderictory(new directoryinfo("c:documents and settingsusernamefavorites"), new directoryinfo("g: emp"))
    end sub