Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi All,

Asked a question earlier on as I was having issues with File permissions trying write a file out to a location. Mehdi Gholam came up with the AppendAllText method this got around the writing issue I was having other issues and got around to check the file, having done around 10 runs I would expect to 10 logs I am seeing two.

VB
'just until I come up with a way of not causing errors
    If ((VoltageTestBool = False) And (TimeKeepTestBool = False) And (LCDtestBool = False) And (RFtestBool = False) And (RFxtalBool = False) And (RFtestHighBool = False) And (QuiescentBool = False)) Then
        Result_Save += "Unit Passed," + DateAndTime.Now
        If (File.Exists(PathFilePass) = False) Then
            CreateText = Result_Save
            File.WriteAllText(PathFilePass, Result_Save + vbCrLf)
        End If
        If (File.Exists(PathFileFail) = True) Then
            MsgBox("here!!")
            File.AppendAllText(PathFilePass, Result_Save + vbCrLf)
        End If
    End If


The part of the routine is shown here, I was under the impression that if the File existed it would add the text to it other wise create it I have changed the second 'if' to an 'elseif' to see if that was the cause of the problem. It isn't. Can anybody shed any light on why this appears to write same information twice and not update the file.

Glenn
Posted

1 solution

From looking at your code I notice the following:

1) You two File.Exists() checks check for two different files, yet within each you are writing to the same location.
2) As a result, if the PathFilePass file exists it overrides any existing file located in "PathFilePass", where as if PathFileFail file exists then it appends text on to it.

I would do them as If Then ElseIf Then End If statements as I would have thought they would be mutually exclusive.

If you want all your outputs then I would do:

VB
If (File.Exists(PathFilePass) = False) Then
                CreateText = Result_Save
                File.WriteAllText(PathFilePass, Result_Save + vbCrLf)
            Else
                File.AppendAllText(PathFilePass, Result_Save + vbCrLf)
            End If


This will result in 1 file that contains all of your output.
Unless you remove the file after each run through.

The check says "If the file is not there, then create it, else append to it.
 
Share this answer
 
Comments
glennPattonWork3 14-Apr-14 9:46am    
Thank You!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900