Unexpected nulls replacing data, possibly while using WriteAllText

Respondida Unexpected nulls replacing data, possibly while using WriteAllText

  • jueves, 02 de agosto de 2012 0:17
     
      Tiene código

    I am attempting to track down a problem where the data in my files is getting replaced with null characters on rare occasions.  Can anyone think of a way that this might happen while using WriteAllText()?    

    I'm wondering if I'm making some basic error when using this logic, or if there is any way it could work but then get corrupted when windows shuts down if I'm missing some kind of cleanup, or if there could be any conflict between the autosave in the timer and the normal save when shutting down that could actually overwrite without raising an error, etc.  Alternatively, if there is a less error-prone way of saving the data to the file, I could try rewriting the file save logic to sidestep the unknown problem.

    Other information that may or may not help:  I notice the error when the application starts at Windows startup.  The data files really do appear to have had their data replaced with (seemingly) random numbers of nulls, the problem is not reading the data files incorrectly.  I might not see the error happen again for a year, although the application is typically used every day.  The logic to write to the files is done both at application shutdown (when closing forms) and also periodically from a timer.

    In case it helps in any way, here is an excerpt from my code, including the call to WriteAllText and also an error check I added which doesn't seem to be catching the problem.  TheText is a System.Windows.Forms.Textbox.

               My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.CombinePath(DataPath, NoteFilename), _
                                                    UnminimizedXPos & vbFormFeed _
                                                    & UnminimizedYPos & vbFormFeed _
                                                    & UnminimizedWidth & vbFormFeed _
                                                    & UnminimizedHeight & vbFormFeed _
                                                    & TheText.Text, False)
    
    
                If Microsoft.VisualBasic.Left(My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.CombinePath(DataPath, NoteFilename)), 1) = Chr(0) Then
                    Throw New System.Exception("File corrupted with null character during saving in ANoteForm.SaveNote")
                End If

    Thanks in advance for any suggestions!


    • Editado sriesch jueves, 02 de agosto de 2012 0:20
    •  

Todas las respuestas

  • jueves, 02 de agosto de 2012 9:50
    Moderador
     
     Respondida

    Hi Sriesch,

    Welcome to the MSDN Forum.

    I have tried this code many times, and I didn't encounter your scenario. So would you like to check the TheText.Text, or just replace it with a const string.

    When you encounter this issue again, please follow up this thread to let us know.

    Thank you very much.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.