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
Popular Posts
-
Now I will show you how to retrieve the data posted from a HTML file in a JSP page. Consider an html page that prompts the user to enter his...
-
Accessing the Standard CGI Variables: To build the successful web application, you often need to know a lot about the environment in which ...
-
INTRODUCTION TO 'C': C is a programming language developed at AT & T's Bell laboratories of USA in 1972.it was designed by d...
-
SQL aliases can be used with database tables and with database table columns, depending on task you are performing. SQL column aliases are u...
-
Learning about Events (Continue) Lets program the Form_Load event. "MsgBox" is Visual Basic command that launch a message box. for...
-
Steps to follow: 1. Write Person.java as shown below in Code-11.3.a under polypackage directory. (You are welcome to do this work using eit...
-
The Command Button's KeyPress, KeyDown and KeyUp Events The events that will be mentioned in the following pages are commonly used, and ...
-
The SQL AVG aggregate function selects the average value for certain table column. Have a look at the SQL AVG syntax: SELECT AVG(Column1) FR...
-
package interfaceexercise; // Define an interface with three abstract methods. // Any class that implements this interface has to // impleme...
-
We will use the Customers table to illustrate the SQL LIKE clause usage: FirstName LastName Email DOB Phone John Smith John.Smith@yaho...
No comments:
Post a Comment