473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Copy a folder/Directory

I have been working on a class that moves files around and have come to a need to move an entire directory at one time. I thought that this would be really simple (some kind of CopyDirectory command) but no such luck. How can you move a Folder/Directory in vb.net

Thank
Brad
Nov 20 '05 #1
4 18241
Hi,

The directoryinfo class has a moveto method to move a directory but
there is no function to copy a directory.

Ken
--------------------
"Brad" <an*******@discussions.microsoft.com> wrote in message
news:E6**********************************@microsof t.com...
I have been working on a class that moves files around and have come to a need to move an entire directory at one time. I thought that this would be
really simple (some kind of CopyDirectory command) but no such luck. How can
you move a Folder/Directory in vb.net?
Thanks
Brad

Nov 20 '05 #2
"Brad" <an*******@discussions.microsoft.com> schrieb
I have been working on a class that moves files around and have come
to a need to move an entire directory at one time. I thought that
this would be really simple (some kind of CopyDirectory command) but
no such luck. How can you move a Folder/Directory in vb.net?


Copy each file. Use System.IO.Directory.GetFiles or
System.IO.DirectoryInfo.GetFiles to get the files/file names.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
* "=?Utf-8?B?QnJhZA==?=" <an*******@discussions.microsoft.com> scripsit:
I have been working on a class that moves files around and have come
to a need to move an entire directory at one time. I thought that this
would be really simple (some kind of CopyDirectory command) but no such
luck. How can you move a Folder/Directory in vb.net?


Move:
'System.IO.Directory.Move'.

Copy:
P/invoke on 'SHFileOperation':
<http://www.palmbytes.de/content/dotnetmisc/dl/shellfileoperation.zip>.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4
Hi Brad,

Of course this can be done.

It's not too easy, because you have to run a recursive routine to get all
the subs and the files they contain.

I wrote several routines in an effort to use this concept to save .net
solutions in order to provide backup copies, which I now use all the time.

I won't bore you with all the details; instead, below is the code that
makes it happen, which I'm sure you'll be able to follow along and work
with:

Imports System.IO

Public Class Form1

Inherits System.Windows.Forms.Form

Public selectedsubs As New ArrayList()

Public subs2 As New ArrayList()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim dirinfo As New DirectoryInfo("c:\vsapps\")

For Each dirinfo In dirinfo.GetDirectories()

solutionsbox.Items.Add(dirinfo.Name)

Next

End Sub

Private Sub saveit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles saveit.Click

Dim dirsys As Directory

Dim copytopath As String

Dim dirfullpath As String

Dim solselectionsarray As String

Dim mdirectory As DirectoryInfo

Dim mfile As FileInfo

Dim dfile As File()

Dim destinationfile As String

Dim solutionsboxfp As New ArrayList()

Dim shortstring, stringfront As String

Dim i, j, startpos As Integer

Me.Cursor = Cursors.WaitCursor

For Each solselectionsarray In solutionsbox.CheckedItems

selectedsubs.Add("c:\vsapps\" & solselectionsarray)

Next

subs2.Clear()

For i = 0 To selectedsubs.Count - 1

mdirectory = New DirectoryInfo(selectedsubs(i))

listdirectories(mdirectory)

Next

For j = 0 To subs2.Count - 1

mdirectory = New DirectoryInfo(subs2(j))

startpos = InStrRev(mdirectory.FullName, "\")

shortstring = Mid(mdirectory.FullName, startpos + 1)

stringfront = Mid(mdirectory.FullName, 11)

If dirsys.Exists("c:\dotnetsource\" & stringfront) Then

Else

dirsys.CreateDirectory("c:\dotnetsource\" & stringfront)

End If

For Each mfile In mdirectory.GetFiles

destinationfile = "c:\dotnetsource\" & stringfront & "\" & mfile.Name

File.Copy(mfile.FullName, destinationfile, True)

Next

Next

Me.Cursor = Cursors.Default

Application.Exit()

End Sub

Sub listdirectories(ByVal thedirectory As DirectoryInfo)

Dim tempdir As DirectoryInfo

subs2.Add(thedirectory.FullName)

For Each tempdir In thedirectory.GetDirectories()

listdirectories(tempdir)

Next

End Sub

End Class

HTH,

Bernie Yaeger
"Brad" <an*******@discussions.microsoft.com> wrote in message
news:E6**********************************@microsof t.com...
I have been working on a class that moves files around and have come to a need to move an entire directory at one time. I thought that this would be
really simple (some kind of CopyDirectory command) but no such luck. How can
you move a Folder/Directory in vb.net?
Thanks
Brad

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Antonio Lopez Arredondo | last post by:
hi all !!! I need to copy a folder and its subfolders to another location; which class should I use ? could only find the System.IO.Directory.MOVE but don't know how to COPY. thanks in...
0
by: Tess | last post by:
Hi, Long time reader, first time poster... Any help is appreciated. I have a few questions regarding Winform controls embedded within an html page. For more info please see the appendix. Now,...
1
by: Johnny Fugazzi | last post by:
I have a relatiovely simple need. I would like to have a webpage that a user can go to, fill out a couple of fields, and then hit OK where the result is that a Folder Template is copied and a...
5
by: Frank | last post by:
I'm using VB .NET and I'm finding that CopyTo will not help me copy all the files in one directory to antoher directory at one time. Which command (if that's the word) do I use to copy ALL the...
2
by: Diffident | last post by:
Hello All, I just finished reading an interesting article by Scott about App Domains: http://odetocode.com/Articles/305.aspx Scott, I have a question about the section "Shadow Copies and...
1
by: Dan | last post by:
I have an application that I want to use for copying files. My goal is to copy a files, if a file is in use or not accessible because of security reasons I want to make note of that file then...
9
by: pamela fluente | last post by:
What is the most current (for framework 2.0) and easy way to copy recursively all files from folder "Folder1" to folder "Folder2" ? Is there any simple function in the framework to do that? ...
4
by: supriyamk | last post by:
Hi, i am trying to copy files of a certain type into a different directory using perl, The copy function doesnt seem to work. my @dir_list; my $sub_dir; my $file_name1; ...
10
by: Jason | last post by:
I want to create a simple program with Two buttons on the form. BUTTON 1 - BACKUP PREFS this will do the following: Copy C:\Documents and Settings\%USERNAME%\Application Data\FileZilla...
3
by: David V | last post by:
I'm having trouble copying DLLs from a folder in my project to the project's output directory. I have copied seven SQL CE DLLs (the SqlCe*.dll set) to a Libs folder in my project. The DLLs VS...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.