VB.Net/Data Structure/Dictionary
Содержание
- 1 Check if a directory does not exist, create a directory
- 2 Create directory with drive letter and without drive letter
- 3 Delete a Directory
- 4 Get and set current working directory
- 5 Get parent directory
- 6 List all Directories under a directory
- 7 List all files under a directory
- 8 Make Dictionary based on your own Object
- 9 Your own Generic List Dictionary
Check if a directory does not exist, create a directory
Imports System.IO
Module Module1
Sub Main()
Try
If (Directory.Exists("D:\test01")) Then
Console.WriteLine("D:\test01 already exists")
Else
Directory.CreateDirectory("C:\test01")
End If
Console.WriteLine("Directories created")
Catch E As Exception
Console.WriteLine("Error creating directory")
Console.WriteLine("Error: {0}", E.Message)
End Try
End Sub
End Module
Create directory with drive letter and without drive letter
Imports System.IO
Module Module1
Sub Main()
Console.WriteLine("Creating directories...")
Directory.CreateDirectory("D:\test01")
Directory.CreateDirectory("D:\Temp\Sample02")
Directory.CreateDirectory("Sample03")
Console.WriteLine("Directories created")
End Sub
End Module
Delete a Directory
Imports System.IO
Module Module1
Sub Main()
Try
Directory.Delete("C:\Sample01", True)
Catch E As Exception
Console.WriteLine("Error deleting directory C:\Sample01")
Console.WriteLine("Error {0}", E.Message)
End Try
End Sub
End Module
Get and set current working directory
Imports System.IO
Module Module1
Sub Main()
Console.WriteLine("Current directory is {0}", Directory.GetCurrentDirectory())
Directory.SetCurrentDirectory("C:\")
Console.WriteLine("Current directory is {0}", Directory.GetCurrentDirectory())
End Sub
End Module
Get parent directory
Imports System.IO
Module Module1
Sub Main()
Dim Current As String
Dim Parent As DirectoryInfo
Try
Current = Directory.GetCurrentDirectory()
Parent = Directory.GetParent(Current)
Console.WriteLine("Current directory {0}", Current)
Console.WriteLine("Parent directory {0}", Parent.FullName)
Catch E As Exception
Console.WriteLine("Error determining parent directory")
Console.WriteLine(E.Message)
End Try
End Sub
End Module
List all Directories under a directory
Imports System.IO
Module Module1
Sub Main()
Dim Root As New DirectoryInfo("C:\")
Dim Files As FileInfo() = Root.GetFiles("*.*")
Dim Dirs As DirectoryInfo() = Root.GetDirectories("*.*")
Console.WriteLine("Root Directories")
Dim DirectoryName As DirectoryInfo
For Each DirectoryName In Dirs
Try
Console.Write(DirectoryName.FullName)
Console.Write(" contains {0} files ", DirectoryName.GetFiles().Length)
Console.WriteLine(" and {0} subdirectories ", DirectoryName.GetDirectories().Length)
Catch E As Exception
Console.WriteLine("Error accessing")
End Try
Next
End Sub
End Module
List all files under a directory
Imports System.IO
Module Module1
Sub Main()
Dim Root As New DirectoryInfo("C:\")
Dim Files As FileInfo() = Root.GetFiles("*.*")
Dim Dirs As DirectoryInfo() = Root.GetDirectories("*.*")
Console.WriteLine("Root Files")
Dim Filename As FileInfo
For Each Filename In Files
Try
Console.Write(Filename.FullName)
Console.Write(" Size: {0} bytes", Filename.Length)
Console.WriteLine(" Last use: {0}", Filename.LastAccessTime)
Catch E As Exception
Console.WriteLine("Error accessing File")
End Try
Next
End Sub
End Module
Make Dictionary based on your own Object
Imports System
Imports System.Collections
Imports System.Collections.Generic
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim dict As New StudentDictionary
dict.Add("113-11-1111", New Student("A", "a"))
dict.Add("222-22-2222", New Student("B", "b"))
dict.Add("333-33-3333", New Student("C", "c"))
dict.Add("444-44-4444", New Student("D", "d"))
Dim student As Student = dict.Item("365-76-5476")
Console.WriteLine(student.ToString)
End Sub
End Class
Public Class Student
Private m_FirstName As String
Private m_LastName As String
Public Sub New(ByVal first_name As String, ByVal last_name As String)
m_FirstName = first_name
m_LastName = last_name
End Sub
Public Overrides Function ToString() As String
Return m_FirstName & " " & m_LastName
End Function
End Class
Public Class StudentDictionary
Inherits System.Collections.DictionaryBase
" Add a Dictionary entry.
Public Sub Add(ByVal new_key As String, ByVal new_student As Student)
Dictionary.Add(new_key, new_student)
End Sub
" Return an object with the given key.
Default Public Property Item(ByVal key As String) As Student
Get
Return DirectCast(Dictionary.Item(key), Student)
End Get
Set(ByVal Value As Student)
Dictionary.Item(key) = Value
End Set
End Property
" Return a collection containing the Dictionary"s keys.
Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property
" Return a collection containing the Dictionary"s values.
Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property
" Return True if the Dictionary contains this Student.
Public Function Contains(ByVal key As String) As Boolean
Return Dictionary.Contains(key)
End Function
" Remove this entry.
Public Sub Remove(ByVal key As String)
Dictionary.Remove(key)
End Sub
End Class
Your own Generic List Dictionary
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.Specialized
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim dict As New StudentListDictionary
dict.Add("111-11-1111", New Student("A", "A"))
dict.Add("333-22-3333", New Student("B", "B"))
dict.Add("444-44-4444", New Student("C", "C"))
dict.Add("555-55-5555", New Student("D", "D"))
Dim student As Student = dict.Item("111-11-1111")
Console.WriteLine(student.ToString)
End Sub
End Class
Public Class Student
Private m_FirstName As String
Private m_LastName As String
Public Sub New(ByVal first_name As String, ByVal last_name As String)
m_FirstName = first_name
m_LastName = last_name
End Sub
Public Overrides Function ToString() As String
Return m_FirstName & " " & m_LastName
End Function
End Class
Public Class StudentListDictionary
Inherits ListDictionary
" Add a Dictionary entry.
Public Shadows Sub Add(ByVal new_key As String, ByVal new_student As Student)
MyBase.Add(new_key, new_student)
End Sub
" Return an object with the given key.
Default Public Shadows Property Item(ByVal key As String) As Student
Get
Return DirectCast(MyBase.Item(key), Student)
End Get
Set(ByVal Value As Student)
MyBase.Item(key) = Value
End Set
End Property
" Return True if the Dictionary contains this Student.
Public Shadows Function Contains(ByVal key As String) As Boolean
Return MyBase.Contains(key)
End Function
" Remove this entry.
Public Shadows Sub Remove(ByVal key As String)
MyBase.Remove(key)
End Sub
End Class