Copy a folder/Directory

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Brad said:
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
 
Hi,

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

Ken
--------------------
Brad said:
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?
 
* "=?Utf-8?B?QnJhZA==?= said:
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>.
 
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 said:
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?
 
Hi

I am facing bit different problem. I am using move method of Directory Class to rename folder to new name
But it is doing some nasty thing i.e. why and how it is removing current session (current session expires) after execution of Move Command

This is small but huge problem to me as my whole application works on session, and user need login again once this code executes

What could be the problem and how can get rid of this

Thanks in advanc
Adhik
 
Back
Top