-
Feb 10th, 2018, 10:12 AM
#1
Thread Starter
Lively Member
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?
-
Feb 10th, 2018, 10:31 AM
#2
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?
-
Feb 10th, 2018, 10:52 AM
#3
Thread Starter
Lively Member
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
-
Feb 10th, 2018, 11:22 AM
#4
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.
Last edited by jmcilhinney; Feb 10th, 2018 at 11:25 AM.
-
Feb 10th, 2018, 11:40 AM
#5
Thread Starter
Lively Member
Re: WriteAllText is changing format unexpectedly
Originally Posted by Penfound
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.
-
Feb 10th, 2018, 10:49 PM
#6
Re: WriteAllText is changing format unexpectedly
Let's take a look at that WriteAllText method:
csharp Code:
public void WriteAllText(string file, string text, bool append) { Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText(file, text, append); }
and that WriteAllTextMethod:
csharp Code:
public static void WriteAllText(string file, string text, bool append) { FileSystem.WriteAllText(file, text, append, Encoding.UTF8); }
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.
-
Feb 11th, 2018, 10:28 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|