Search | Contact | Link To Us  


      VB .NET       

.NET Defined
OOP with VB
VB Language
Win Forms
Windows Controls
ADO .NET
User Controls
File Handling
Multithreading
Deployment
XML Web Services Essential XML
Resources
ASP.NET
About



Advertisement

visual basic, files in vb.net, .net directories, file handling in .net, .net streams, .net files,.net deployment, deploying .net applications, .net languages, microsoft .net, .net assemblies, .net controls, microsoft languages, programming, progrmmaing .net, programming vb.net, compiling assemblies, compile .net applications,programming visual basic.net








     Home> File Handling> Directories in VB .NET


Files in VB .NET

Working with Directories

We will work with the File and Directory classes in this section. We will create a directory and copy a file into the newly created directory.

File Class

The File class in VB .NET allows us to work with files, allowing to copy, delete and create files.

Directory Class

The Directory class in VB .NET allows us to create and work with Folders/Directories. With this class we can create, edit and delete folders and also maintain drives on the machine.

Code to work with File and Directory Class

The following code will create a directory and copy a file into that new directory. To create a new directory we should use the Directory class's CreateDirectory method. Drag two Button's (Button1, Button2), a TextBox and a OpenFileDialog control from the toolbar onto the Form. We will use the TextBox to specify a location to create the Directory when Button1 is clicked and the OpenFileDialog control to open a file and copy it into the newly created Directory when Button2 is clicked. The namespace to be imported is System.IO. The code for that looks like this:

Imports System.IO
Public Class Form1 Inherits System.Windows.Forms.Form

 'Windows Form Designer Generated Code

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

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click

Try
Directory.CreateDirectory(TextBox1.Text)
 'Creating a directory by specifying a path in the TextBox, of the form c:\examples
'Instead of using a TextBox you can directly type the location of the directory like this
'Directory.CreateDirectory("c:\examples")
Catch
End Try
MsgBox("Done")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button2.Click
Try
If OpenFileDialog1.ShowDialog <> DialogResult.Cancel Then
File.Copy(OpenFileDialog1.FileName, TextBox1.Text & "\" & _
OpenFileDialog1.FileName.Substring(OpenFileDialog1.FileName.LastIndexOf("\")))
'The above line of code uses OpenFileDialog control to open a dialog box where you
'can select a file to copy into the newly created directory
End If
Catch
End Try
MsgBox("File Copied Successfully")
End Sub
End Class

That's all it takes to create a directory and copy a file into the newly created directory in VB.NET. The image below shows output from above code.

The image below shows the directory I created and the file I copied to the new directory using the code above.


 

 

  Privacy Policy | Terms of Use | Site Map | Contact

  © 2004-2010 Startvbdotnet.com. All rights reserved.