Results 1 to 7 of 7

Thread: WriteAllText is changing format unexpectedly

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    91

    WriteAllText is changing format unexpectedly

    Hi guys - I've been working on ths problem for a couple of days now and tried many different solutions floating around on the internet with out success.

    I have some text boxes with neatly formatted line of data such as:

    1,1,2,C3,C140,C141,C142,C143,C144,C146
    2,3,4,C5
    3,5,69,C6,C74
    4,6,7,C9,C11,C12,C85,C86,C88,C89,C91,C92

    However this code

    <code>,
    TextBox4.Text = FamilyDetails.ToString
    My.Computer.FileSystem.WriteAllText("H:\VB Project\fred\data\families.csv", TextBox4.Text, False)
    </code>

    produces this


    1,
    1,
    2,C
    3,C
    140,C
    141,C
    142,C
    143,C
    144,C
    146

    2,
    3,
    4,C
    5 ...etc

    This is not then read correctly by

    <code>
    Using MyReader As New FileIO.TextFieldParser("H:\VB Project\fred\data\years.csv", System.Text.Encoding.UTF8)
    </code>

    I have tried enclosing each field bother double and single quotes without success.

    Please can someone help?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: WriteAllText is changing format unexpectedly

    Unless something is corrupt in your project or on your computer, what you describe simply wouldn't happen. I suspect that the TextBox doesn't contain what you think it does. How EXACTLY did you populate the TextBox in the first place?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    91

    Re: WriteAllText is changing format unexpectedly

    Hi

    I have one Textbox to display a GEDCOM File which is then parsed into an Individuals file and a Families file using Select Case statements like:

    <code>
    Case strLine.Contains("1 FAMC @F")
    'this person is a child in a family
    strLine = strLine.Replace("1 FAMC @F", "")
    strLine = strLine.Replace("@", "")
    IndDetails.Append(ControlChars.Quote & strLine & ControlChars.Quote & ",")
    </code>
    The stringBuilders then pass their contents into textboxes

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: WriteAllText is changing format unexpectedly

    That doesn't really make sense. Your code shows that you are adding double quotes into the text and yet there are no quotes anywhere in your first post. The code you have provided simply cannot produce the result you showed, so you're telling us fibs. Please provide a code example that we can execute that demonstrates the issue.

    EDIT: There's also no way for us to know that the original text doesn't contain line breaks. One thing you could do is set Multiline to True on your TextBox and see if the text is displayed as you see it in the file. If you do then that's obviously the issue, i.e. the text is as you see it in the file and it's simply that you're using a TextBox that specifically strips line breaks that you're not seeing that in your UI.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    91

    Re: WriteAllText is changing format unexpectedly

    Quote Originally Posted by Penfound View Post
    Hi guys - I've been working on ths problem for a couple of days now and tried many different solutions floating around on the internet with out success.

    I have some text boxes with neatly formatted line of data such as:

    1,1,2,C3,C140,C141,C142,C143,C144,C146
    2,3,4,C5
    3,5,69,C6,C74
    4,6,7,C9,C11,C12,C85,C86,C88,C89,C91,C92

    However this code

    <code>,
    TextBox4.Text = FamilyDetails.ToString
    My.Computer.FileSystem.WriteAllText("H:\VB Project\fred\data\families.csv", TextBox4.Text, False)
    </code>

    produces this


    1,
    1,
    2,C
    3,C
    140,C
    141,C
    142,C
    143,C
    144,C
    146

    2,
    3,
    4,C
    5 ...etc

    This is not then read correctly by

    <code>
    Using MyReader As New FileIO.TextFieldParser("H:\VB Project\fred\data\years.csv", System.Text.Encoding.UTF8)
    </code>

    I have tried enclosing each field bother double and single quotes without success.
    I have found where the problem is now. It was the WriteAllText function. I'm now using the StreamWriter and it is working perfectly.
    Thanks for your help though.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: WriteAllText is changing format unexpectedly

    Let's take a look at that WriteAllText method:
    csharp Code:
    1. public void WriteAllText(string file, string text, bool append)
    2. {
    3.   Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText(file, text, append);
    4. }
    and that WriteAllTextMethod:
    csharp Code:
    1. public static void WriteAllText(string file, string text, bool append)
    2. {
    3.   FileSystem.WriteAllText(file, text, append, Encoding.UTF8);
    4. }
    and that WriteAllText method:
    Code:
        public static void WriteAllText(string file, string text, bool append, Encoding encoding)
        {
          FileSystem.CheckFilePathTrailingSeparator(file, nameof (file));
          StreamWriter streamWriter = (StreamWriter) null;
          try
          {
            if (append && File.Exists(file))
            {
              StreamReader streamReader = (StreamReader) null;
              try
              {
                streamReader = new StreamReader(file, encoding, true);
                char[] buffer = new char[10];
                streamReader.Read(buffer, 0, 10);
                encoding = streamReader.CurrentEncoding;
              }
              catch (IOException ex)
              {
              }
              finally
              {
                if (streamReader != null)
                  streamReader.Close();
              }
            }
            streamWriter = new StreamWriter(file, append, encoding);
            streamWriter.Write(text);
          }
          finally
          {
            if (streamWriter != null)
              streamWriter.Close();
          }
        }
    I've highlighted the parts that will get executed in your case. As you can see, it was already using a StreamWriter. Plenty of people call that WriteAllText method without issue. The actual issue is something that you were doing. We may never know exactly what that was now, if you can't show us how to reproduce the issue.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: WriteAllText is changing format unexpectedly

    By the way, the button to put [CODE][/CODE] tags in is the # button. You came close with <code>, but just slightly off.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width