Visual Basic 2008 Development Language May Not Be WriteAllText in .net

Asked 2 years ago, Updated 2 years ago, 53 views

Using the My.Computer.FileSystem.WriteAllText function, we output the contents of String.

I output grid information as if it were separated by commas, but data might be missing in the middle.

Reproducibility may not be 100%, but may also depend on the terminal.

Dim fileName as String="C:\work\output.csv"

for as integer = 0 to grid.RowCount-1

 DimstrData as String=GetData()

 My.Computer.FileSystem.WriteAllText(fileName, strData, True, Encoding.GetEncoding("shift_jis"))

Next

If the number of data you want to output is 600, the actual number of data output may be 598 or 599.

Missing data does not seem to be the same and regular every time.

I am writing data obtained by a different function from GetData(), but when I logged out strData.length, it did not reach zero, so I understand that I was able to retrieve the data.

I replaced it with StreamWriter, but a similar symptom occurred.

For StreamWriter, try Open/Close in the For statement.

I am asking you a question because I do not know what is the reason and what is good as a countermeasure.

We have created code for the 2021/09/27 test.

'Existing files you want to print (add them to one file one after another)
Dim writer As System.IO.StreamWriter = New System.IO.StreamWriter(vFile, True, Encoding.GetEncoding("shift_jis")
writer.WriteLine(strLine)
writer.Close()

US>'Debugging
'Generate file name using grid information and write String information
Dim wFileNameAsString=i+1&"_"&.Item(0,i).Value&"_"&.Item(1,i).Value&."csv"

'StreamWriter
Dim testWrite As System.IO.StreamWriter = New System.IO.StreamWriter(wRoot&wStrWriterPath&"\"&wFileName, True, Encoding.GetEncoding("shift_jis")
testWrite.WriteLine(strLine)
testWrite.Close()

'FileSystem WriteAllText
My.Computer.FileSystem.WriteAllText(wRoot&wFileAllTextPath&"\"&wFileName, strLine, True, Encoding.GetEncoding("shift_jis"))

'File WriteAllText
System.IO.File.AppendAllText(wRoot&wFileAppendPath&"\"&wFileName, strLine, Encoding.GetEncoding("shift_jis"))

The above three methods were used for output.
You can see all lines of output in the individual output grid line by line, but
In some cases, two lines were missing when all lines were output to one file.
There are 830 lines of information in the grid.

Each output contains 830 files and data, but
There were only 828 lines in the full line output of one file.

.net

2022-09-30 17:06

1 Answers

First, .NET has the source code published.

< u l >
  • FileSystem.WriteAllText
  • File.AppendAllText
  • Both methods have been tried, but both are only calling StreamWriter internally.Of course

    < u l >
  • StreamWriter
  • It is also open to the public.The link is from .NET 5.0, and the question is Visual Basic 2008 (around .NET Framework 2.0 to 3.5) with different versions, but the basic implementation is the same.
    (Previously, the .NET Framework 2.0 or later source code was also published, but the distribution ended...)

    I honestly feel that this StreamWriter is the basis of file I/O, and I don't think there are any bugs.For example, Azulean suggested that we avoid frequent open/close.Of course, you are right to avoid frequent open/close, but StreamWriter is designed to throw exceptions if something goes wrong, even if it happens frequently.Currently, if no exceptions have been thrown, no such problem has occurred and it should be working as instructed.
    (In that sense, On Error Resume Next does not ignore the exception, does it?)

    Rather, this kind of question is more likely to be due to a cause outside the scope of the question.The first comment I made was that the number of loops was different than expected, there was also a difference in the number of lines in the output file, the encoding was incorrect, and the antivirus software rewritten the contents of the file.These are some of the examples that came to mind, and there is no point in being individually denied.The possibilities are endless, and only the questioner can understand them.

    Therefore, rather than suspecting StreamWriter other behaviors, I would recommend that you look outside.As a questioner, you may not understand, but this site is intended to accumulate Q&A, and from that point of view, it is beneficial for those who suspect StreamWriter and so on.


    2022-09-30 17:06

    If you have any answers or tips


    © 2024 OneMinuteCode. All rights reserved.