.NET Programming With Me

VB.NET : Copying files from one folder to another

The interface of my Application used a font available in Windows 7 but not in Windows XP. While opening the Application, it would not show nicely (in other words, my forms were disordered esp. @ bold controls). 

So, I needed the same fonts in Windows XP or the system not having those fonts.
I did what we would do to solve the problems from my Application itself, copying those fonts to the Windows Fonts folder.

Private Sub CopyFont()
        Dim fonts(5) As String 'To store fonts name inside the folder
        Dim i As Integer = 0
        Dim fontExists As Boolean = False
        Dim sourcePath As String = String.Empty
        Dim destinationPath As String = String.Empty
        Dim fontNamewithPath As String = String.Empty
        Dim fontName As String = String.Empty

        ' Listing all fonts found in "..\Fonts\" folder
        For Each foundFontFile As String In My.Computer.FileSystem.GetFiles(Application.StartupPath & "\Fonts\", FileIO.SearchOption.SearchTopLevelOnly, "*.ttf")
            fontNamewithPath = foundFontFile
            Dim font() As String = fontNamewithPath.Split("\")
            fontName = font(font.Length() - 1)

            fonts(i) = fontName.Trim(" ")
            i += 1
        Next

        ' Verifying whether the destined folder contains all the fonts
        For Each requiredFont In fonts
            For Each oldFont As String In My.Computer.FileSystem.GetFiles("C:\Windows\Fonts\", FileIO.SearchOption.SearchTopLevelOnly, requiredFont)
                fontNamewithPath = oldFont
                Dim font() As String = fontNamewithPath.Split("\")
                fontName = font(font.Length() - 1)

                If fontName = requiredFont Then
                    fontExists = True
                End If
            Next
        Next

        ' If the destined folder doesn't contains the fonts of the source folder, copy it to the destined folder
        If fontExists = False Then
            For Each requiredFont In fonts
                sourcePath = Application.StartupPath & "\Fonts\" & requiredFont
                destinationPath = "C:\Windows\Fonts\" & requiredFont
                My.Computer.FileSystem.CopyFile(sourcePath, destinationPath, FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing)
            Next
        Else
            'MessageBox.Show("Font already exists", "Install Fonts")
            'Do Nothing
        End If

    End Sub


P.S. I did the above possible with some googling and some stackoverflow posts...

No comments: