Retrieves The Default User's Directory Path

 Introduction

To receive the path of the user's default directory, the following function(s) may be applied.

    Public Shared Function GetDefaultDirectory() As String
        Dim txt As String = ""
        Dim paths As String() = Environment.GetFolderPath(Environment.SpecialFolder.Personal).Split(New Char() {"\"c})
        For i As Integer = 0 To paths.Length - 2
            txt = txt & paths(i) & "\"
        Next
        Dim defaultDirectory As String = Nothing
        Try
            If Not Directory.Exists(txt) Then
                Directory.CreateDirectory(txt)
            End If
            defaultDirectory = txt
        Catch ex As Exception
            If TypeOf ex Is IOException AndAlso TypeOf ex Is UnauthorizedAccessException AndAlso TypeOf ex Is ArgumentException Then
                defaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
            End If
        End Try
        Return defaultDirectory
    End Function


and in addition you may retrieve the Documents, Videos, Music and Pictures  directories using the below functions.


    Public Shared Function GetDocumentsDirectory() As String
        Dim txt As String = GetDefaultDirectory()
        txt += "Documents"
        Return txt
    End Function

    Public Shared Function GetVideoDirectory() As String
        Dim txt As String = GetDefaultDirectory()
        txt += "Videos"
        Return txt
    End Function

    Public Shared Function GetMusicDirectory() As String
        Dim txt As String = GetDefaultDirectory()
        txt += "Music"
        Return txt
    End Function

    Public Shared Function GetPicturesDirectory() As String
        Dim txt As String = GetDefaultDirectory()
        txt += "Pictures"
        Return txt
    End Function

 Code Usage :

   ' Example to Get Documents Directory
        Dim documentsDirectory As String = GetDocumentsDirectory()
        ' do whatever you want with the code


Enjoy it...................

Comments

Popular posts from this blog

مقدمة الي تشفير الحروف الأبجدية العربية

VB.NET Translucent Control using GDI+

Add Custom Event to a Class in VB.NET