File directory operationst in VB.net
By: Issac in VB.net Tutorials on 2009-01-30
Directory class in .net is of static nature we need not have to instantiate the class, and can direcly call methods in the class directly from the directory class.
How to create a directory?
In order to create a new directory, we can call CreateDirectory directly from Directory class.
Syntax: Directory.CreateDirectory (DirPath)
DirPath: The name of the new directory
VB.NET: Directory.CreateDirectory ("c:\testdir")
How to check a directory exist or not?
Before we create a directory, we usually check whether that directory exist or not. For that we are using the Exists method in the Directory class.
Syntax : Directory.Exists(DirPath) as Boolean
DirPath : The name of the directory
Boolean : Returns true or false , if directory exist it Returns true , else it Returns false
VB.NET: Directory.Exists (">c:\testdir")
How to move a Directory?
We can use Move method in the Directory class to move a directory and its contents from one location to another
Syntax : Move(sourceDirName,destDirName)
sourceDirName : The source directory we are going to move.
destDirName : The destination directory name.
VB.NET: Directory.Move ("c:\testdir1\testdir2", ">c:\testdir")
How to delete a Directory ?
When we want to delete a directory we can use the Delete method in the Directory class
Syntax : Delete(DirPath)
DirPath : The Directory we want to delete.
VB.NET : Directory.Delete("c:\testdir1")
The following VB.NET source code shows these operations :
Imports System.IO Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles Button1.Click If Directory.Exists("c:\testDir1") Then 'shows message if testdir1 exist MsgBox("Directory 'testDir' Exist ") Else 'create the directory testDir1 Directory.CreateDirectory("c:\testDir1") MsgBox("testDir1 created ! ") 'create the directory testDir2 Directory.CreateDirectory("c:\testDir1\testDir2") MsgBox("testDir2 created ! ") 'move the directory testDir2 as testDir in c:\ Directory.Move("c:\testDir1\testDir2", "c:\testDir") MsgBox("testDir2 moved ") 'delete the directory testDir1 Directory.Delete("c:\testDir1") MsgBox("testDir1 deleted ") End If End Sub End Class
When you run this program you can see, first it create directory testDir1 and then testDir2 is creating inside testDir1, Next the program move the testDir2 to testDir. Finally it deletes the directory testDir1. After the execution you can see testDir in c:\
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Throwing an Exception in VB.net
Comments