Wednesday, May 26, 2010

How to Create, Copy, Move, Rename, Delete File or Directory VB.net or Visual Basic

Hello and welcome to another cool vb.net tutorial. Today in this tutorial i will be teaching u all how to use the Directory or File Operations in Vb.net or Visual Basic



Requirements:
4 Timers (Name: Timer1, Timer2, Timer3, Timer4) (Interval: 150, 1000, 1000, 1000)
1 Label (Name: Label1) (Text: Status.)
8 Buttons (Name: Button1, Button2, Button3,Button4, Button5, Button6, Button7, Button8)(Text: Create Directory, Move Directory, Delete Directory, Create File, Copy File, Delete File, Rename File)

Codes:

Imports System.IO
Public Class Form1

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
Timer1.Enabled = True
Timer2.Enabled = True
Timer3.Enabled = True
Timer4.Enabled = True
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = "Status"
Timer1.Enabled = False
Timer2.Enabled = True
End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Label1.Text = "Status."
Timer2.Enabled = False
Timer3.Enabled = True
End Sub

Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
Label1.Text = "Status.."
Timer3.Enabled = False
Timer4.Enabled = True
End Sub

Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
Label1.Text = "Status..."
Timer4.Enabled = False
Timer1.Enabled = True
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim path As String = "c:\test"
If Directory.Exists(path) Then
Directory.Delete(path)
Label1.Text = "Directory Deleted Successfully"
Else
Label1.Text = "Directory Not Found"
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
If Not Directory.Exists("c:\test") = True Then
Label1.Text = "Directory Does Not Exist..Program will now create a Directory..!"
Directory.CreateDirectory("c:\test\")
Label1.Text = "Directory Created Successfully"
Else
Label1.Text = "Directory Already Exist..!"
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
Dim destination As String = "c:\hello"
Dim path As String = "c:\test"
If Directory.Exists(destination) Then
Label1.Text = "Folder with same name already exist in the destination "
ElseIf Directory.Exists(path) Then
Directory.Move(path, destination)
Label1.Text = "Directory Moved Successfully"
Else
Label1.Text = "Directory Already Exist in the location specified"
End If
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
Dim oldname As String = "c:\hello"
Dim newname As String = "c:\Subscribe"
If Directory.Exists(oldname) And Not Directory.Exists(newname) Then
Directory.Move(oldname, newname)
Label1.Text = "Renaming was Successfull"
Else
Label1.Text = "There is folder already with same name in the Directory"
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
Dim filetocreate As String = "c:\test.txt"
If Not File.Exists(filetocreate) Then
File.Create(filetocreate)
Label1.Text = "File Created Successfully"
Else
Label1.Text = "File Already Exist"
End If
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
Dim path As String = "c:\test.txt"
Dim destination As String = "c:\Subscribe\test.txt"
If File.Exists(path) And Not File.Exists(destination) Then
File.Copy(path, destination)
Label1.Text = "File Copied Successfully"
Else
Label1.Text = "There is already a file called test.txt in the Destination Folder"
End If
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
Dim filetodelete As String = "c:\test.txt"
If File.Exists(filetodelete) Then
File.Delete(filetodelete)
Label1.Text = "File Deleted Successfully"
Else
Label1.Text = "File Not Found"
End If
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
Dim oldname As String = "c:\Subscribe\test.txt"
Dim newname As String = "c:\Subscribe\Subscribe Please.txt"
If File.Exists(oldname) And Not File.Exists(newname) Then
File.Move(oldname, newname)
Label1.Text = "Renaming Was Successfull"
Else
Label1.Text = "File Already Exist with the same name"
End If
End Sub

End Class


Here is the video tutorial:



1 comment:

  1. Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. http://psiprograms.com

    ReplyDelete