Quantcast
Channel: .Net Core | MVC | HTML Agility Pack | SQL | Technology Crowds
Viewing all articles
Browse latest Browse all 544

How to write in Text file on desktop in vb.net

$
0
0

We can use System.Environment.GetFolderPath to get the desktop folder.
If particular folder is not created on desktop it will create. if the write to text file already exists then it delete and again write the text file.


      Public Function WriteToText(ByVal obj As String, ByVal FileName As String) As Boolean
            Dim FilePath As String

            FilePath = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
            FilePath &= "\Sample\Test\"

            Try
                If Not System.IO.Directory.Exists(FilePath) Then
                    Dim di As DirectoryInfo = System.IO.Directory.CreateDirectory(FilePath)
                End If

                FilePath &= FileName
                If System.IO.File.Exists(FilePath) = True Then
                    System.IO.File.Delete(FilePath)
                End If

                Dim fi As FileInfo = New FileInfo(FilePath)
                Dim sw As StreamWriter
                If Not fi.Exists Then
                    sw = fi.CreateText()
                    sw.Write(obj)
                    sw.Flush()
                    sw.Close()
                End If
            Catch ex As Exception
                MessageBox.Show("Writting to test folder error occurred!" + ex.Message.ToString)
                Return False
            End Try
            Return True
        End Function


Viewing all articles
Browse latest Browse all 544

Trending Articles