473,545 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18252
Hi,

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

Ken
--------------------
"Brad" <an*******@disc ussions.microso ft.com> wrote in message
news:E6******** *************** ***********@mic rosoft.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*******@disc ussions.microso ft.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.Direc tory.GetFiles or
System.IO.Direc toryInfo.GetFil es 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*******@disc ussions.microso ft.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.Dire ctory.Move'.

Copy:
P/invoke on 'SHFileOperatio n':
<http://www.palmbytes.d e/content/dotnetmisc/dl/shellfileoperat ion.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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

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

For Each dirinfo In dirinfo.GetDire ctories()

solutionsbox.It ems.Add(dirinfo .Name)

Next

End Sub

Private Sub saveit_Click(By Val sender As System.Object, ByVal e As
System.EventArg s) Handles saveit.Click

Dim dirsys As Directory

Dim copytopath As String

Dim dirfullpath As String

Dim solselectionsar ray 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.WaitCur sor

For Each solselectionsar ray In solutionsbox.Ch eckedItems

selectedsubs.Ad d("c:\vsapps\ " & solselectionsar ray)

Next

subs2.Clear()

For i = 0 To selectedsubs.Co unt - 1

mdirectory = New DirectoryInfo(s electedsubs(i))

listdirectories (mdirectory)

Next

For j = 0 To subs2.Count - 1

mdirectory = New DirectoryInfo(s ubs2(j))

startpos = InStrRev(mdirec tory.FullName, "\")

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

stringfront = Mid(mdirectory. FullName, 11)

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

Else

dirsys.CreateDi rectory("c:\dot netsource\" & stringfront)

End If

For Each mfile In mdirectory.GetF iles

destinationfile = "c:\dotnetsourc e\" & stringfront & "\" & mfile.Name

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

Next

Next

Me.Cursor = Cursors.Default

Application.Exi t()

End Sub

Sub listdirectories (ByVal thedirectory As DirectoryInfo)

Dim tempdir As DirectoryInfo

subs2.Add(thedi rectory.FullNam e)

For Each tempdir In thedirectory.Ge tDirectories()

listdirectories (tempdir)

Next

End Sub

End Class

HTH,

Bernie Yaeger
"Brad" <an*******@disc ussions.microso ft.com> wrote in message
news:E6******** *************** ***********@mic rosoft.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
3322
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 advance, ant
0
2577
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, for the questions. 1. A button on my control executes the System.IO.Directory.GetDirectories funtion (the scanned directory resides on the hosting...
1
1305
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 new folder is created. The folders in question are on a IIS based FTP server. I am locking down the folder structure in an attempt to keep users...
5
15079
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 files at one to another directory? -- Thanks (Novice)
2
2233
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 Restarts". You talked about "Drain Stopped" and "Shadow Copy" concepts in this article. I maintain a web application which is in production. Every...
1
2727
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 continue with the file copy process. For some reason when my app is copying the C:\Windows\System32\Config folder it will fail on the first...
9
5697
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? -P
4
1650
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; my $file_name2; my @file_list_gel; my @file_list_res;
10
4946
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 \sitemanager.xml to this location: \\drake\pvt\%USERNAME%\FileZilla-Prefs\ BUTTON 2 - RESTORE PREFS this will do the opposite:
3
4616
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 2008 file properties are set as follows: Build Action: Content Copy to Output Directory: Always When I compile, the DLLs don't copy to the output...
0
7390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7647
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7410
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5318
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4940
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1865
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1007
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
692
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.