Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Create Text file as ANSI encoding Format

Create Text file as ANSI encoding Format

Scheduled Pinned Locked Moved Visual Basic
helpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rupesh Kumar Swami
    wrote on last edited by
    #1

    hi, i create text file through following code here argument FilePath contain full path of file and Data contains Contents which has been written to text file. if file already exist then it add data to previous contents of file otherwise it create a new text file Private Sub CreateTextFile(ByVal FilePath As String, ByVal data As String) Dim oFile As System.IO.File Dim oWrite As System.IO.StreamWriter Dim oRead As System.IO.StreamReader Dim PreviousData As String = "" Dim CheckHypen As String If File.Exists(FilePath) Then oRead = oFile.OpenText(FilePath) While oRead.Peek <> -1 If PreviousData = "" Then PreviousData = oRead.ReadLine() Else PreviousData = PreviousData & ControlChars.NewLine & oRead.ReadLine() End If End While oRead.Close() End If oWrite = oFile.CreateText(FilePath) If PreviousData = "" Then oWrite.WriteLine(data) Else oWrite.WriteLine(PreviousData & ControlChars.NewLine & data) End If oWrite.Close() End Sub i don't specify encoding format . some time text file is created with ANSI encoding format and sometime UTF-8 encoding format(dependent upon content). my problem is that i strictly create file as ANSI encoding.how it is possible? How can i set text file Encoding format to ANSI? any help will be appreciated.

    Rupesh Kumar Swami Software Engineer, Integrated Solution, Bikaner (India)

    I 1 Reply Last reply
    0
    • R Rupesh Kumar Swami

      hi, i create text file through following code here argument FilePath contain full path of file and Data contains Contents which has been written to text file. if file already exist then it add data to previous contents of file otherwise it create a new text file Private Sub CreateTextFile(ByVal FilePath As String, ByVal data As String) Dim oFile As System.IO.File Dim oWrite As System.IO.StreamWriter Dim oRead As System.IO.StreamReader Dim PreviousData As String = "" Dim CheckHypen As String If File.Exists(FilePath) Then oRead = oFile.OpenText(FilePath) While oRead.Peek <> -1 If PreviousData = "" Then PreviousData = oRead.ReadLine() Else PreviousData = PreviousData & ControlChars.NewLine & oRead.ReadLine() End If End While oRead.Close() End If oWrite = oFile.CreateText(FilePath) If PreviousData = "" Then oWrite.WriteLine(data) Else oWrite.WriteLine(PreviousData & ControlChars.NewLine & data) End If oWrite.Close() End Sub i don't specify encoding format . some time text file is created with ANSI encoding format and sometime UTF-8 encoding format(dependent upon content). my problem is that i strictly create file as ANSI encoding.how it is possible? How can i set text file Encoding format to ANSI? any help will be appreciated.

      Rupesh Kumar Swami Software Engineer, Integrated Solution, Bikaner (India)

      I Offline
      I Offline
      iprasad007
      wrote on last edited by
      #2

      Following one line will do the all above mentioned task including appending and all ... My.Computer.FileSystem.WriteAllText("c:\x.txt", ControlChars.NewLine & "あだsだsd", True, System.Text.Encoding.Default) Third parameter "True" specifies appending and fourth parameter specifies ANSI depending upon ur systems ANSI code page.

      R 1 Reply Last reply
      0
      • I iprasad007

        Following one line will do the all above mentioned task including appending and all ... My.Computer.FileSystem.WriteAllText("c:\x.txt", ControlChars.NewLine & "あだsだsd", True, System.Text.Encoding.Default) Third parameter "True" specifies appending and fourth parameter specifies ANSI depending upon ur systems ANSI code page.

        R Offline
        R Offline
        Rupesh Kumar Swami
        wrote on last edited by
        #3

        hi prasad, thanks for your support. i recently find method (AppendAllText) on internet during searching.i want to ask one more thing what is the difference between My.Computer.FileSystem.WriteAllText and AppendAllText method(of system.io.file object) can i use AppendAllText method ? thanks in advance

        Rupesh Kumar Swami Software Engineer, Integrated Solution, Bikaner (India)

        I 1 Reply Last reply
        0
        • R Rupesh Kumar Swami

          hi prasad, thanks for your support. i recently find method (AppendAllText) on internet during searching.i want to ask one more thing what is the difference between My.Computer.FileSystem.WriteAllText and AppendAllText method(of system.io.file object) can i use AppendAllText method ? thanks in advance

          Rupesh Kumar Swami Software Engineer, Integrated Solution, Bikaner (India)

          I Offline
          I Offline
          iprasad007
          wrote on last edited by
          #4

          Hi Rupesh, Ofcource you can use it. Methods in My.Computer.FileSystem provides easy to use wrapper function to use the functionality implimented in System.Io.File. Regards Prasad

          R 1 Reply Last reply
          0
          • I iprasad007

            Hi Rupesh, Ofcource you can use it. Methods in My.Computer.FileSystem provides easy to use wrapper function to use the functionality implimented in System.Io.File. Regards Prasad

            R Offline
            R Offline
            Rupesh Kumar Swami
            wrote on last edited by
            #5

            Thanks a lot

            Rupesh Kumar Swami Software Engineer, Integrated Solution, Bikaner (India)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups