|
-
Nov 24th, 2009, 10:41 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Crash when overwriting using StreamWriter.
I'm getting a problem when writing text files using StreamWriter. The files read fine and they write fine if I'm not overwriting an existing file. They also write ok as long as I'm not writing over a file that only has one digit at the end of the name. For some reason when there are 2 digits at the end it crashes when I'm trying to save over an already existing text file. Here's my code.
Code:
StringForTextFile = "C:\" & Folder4PS & "\" & IDCodewDashOrN & "Series" & Series & "ScreenNumber" & ScreenNumber & ".txt"
LblTesting.Visible = True ' Testing
LblTesting.Text = "StringForTextFile = " & StringForTextFile
If File.Exists(StringForTextFile) = True Then
Dim PerScrnsData As StreamWriter ' False becasue will overwrite over already existing file
PerScrnsData = New StreamWriter(StringForTextFile, False, Encoding.UTF8) '
AcceptSettings()
PerScrnsData.Write(PersonalScreensDataToStringAll())
PerScrnsData.Close()
Else
Dim PerScrnsData As StreamWriter ' True because will write new file that doesn't exist
PerScrnsData = New StreamWriter(StringForTextFile, True, Encoding.UTF8) '
AcceptSettings()
PerScrnsData.Write(PersonalScreensDataToStringAll())
PerScrnsData.Close()
End If
I have PerScrnsData = New StreamWriter(StringForTextFile, False, Encoding.UTF8) above but 've tried all the different Encoding possibilites. Unicode, UTF32, UTF8, UTF7 as well as without those parameters listed where the close parentheses is right after StringForTextFile. I get the same result. If I'm writing a new text file that doesn't exist, then no problem or if I'm writing over an existing text file but it has 0 to 9 at the end then I'm ok but if I'm writing over an existing file and there is 10 or above(haven't tried triple digits or all values) then my application crashes. You see the test label in the code above because I was looking at exactly what file it was trying to overwrite and it is just as it should be.
Should I be using a different procedure? An example of a string that would cause a crash for the string StringForTextFile would be :
C:\Name of My App\Series1ScreenNumber10.txt
If it was being written for the first time then no problem or if the 10 at the end was 0 to 9 then no problem also.
I've also tried and originally had it set up where there wasn't an If File.Exists(StringForTextFile) = True Then statement. It just always used the same code whether writing or overwriting and I wasn't having a problem that I remember. For some reason I started having a problem recently and I put the alternate methods of writing in.
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
-
Nov 24th, 2009, 10:50 PM
#2
Thread Starter
Fanatic Member
Re: Crash when overwriting using StreamWriter.
I just thought of something simple. Just delete the file first if it already exists and then write it new. I'll look into how to delete the file if it already exists.
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
-
Nov 24th, 2009, 10:57 PM
#3
Thread Starter
Fanatic Member
Re: Crash when overwriting using StreamWriter.
Very simple indeed. If file already exists then :
Code:
My.Computer.FileSystem.DeleteFile(StringForTextFile)
If someone knows why it was only having a problem overwriting with 10 or above at the end that might be interesting to know why. You just need one good way to fix a problem.
Thank you EntityX for solving my problem. You're welcome.
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
-
Nov 24th, 2009, 11:05 PM
#4
Re: [RESOLVED] Crash when overwriting using StreamWriter.
If it crashes then it gives you an error message. If you'd like us to help you fix the problem then we shouldn't really have to ask for you to tell us what it is.
Apart from that, if you want to overwrite an existing file then differentiating between a new and existing file is unnecessary. Did you read the documentation for the StreamWriter constructor?
If the file exists and append is false, the file is overwritten. If the file exists and append is true, the data is appended to the file. Otherwise, a new file is created.
If there's no existing file then a file gets created either way, so forget checking and just set append to False.
-
Nov 25th, 2009, 02:07 AM
#5
Thread Starter
Fanatic Member
Re: [RESOLVED] Crash when overwriting using StreamWriter.
I was trying both ways and it was crashing. I turned unmanaged code debugging on. Before I did that it would show me the problem line but not any message.
If I put the code back like it was before, I get this message:
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: Access to the path 'C:\Ray Rover Personal Screens\Series1ScreenNumber10.txt' is denied.
Deleting the file if it exists works just fine but why would it only have this problem if the file already exists and the number at the end is 10 or above. There's no problem if there is 0 to 9 at the end.
Here's the code I'm using now that solves my problem.
Code:
StringForTextFile = "C:\" & Folder4PS & "\" & IDCodewDashOrN & "Series" & Series & "ScreenNumber" & ScreenNumber & ".txt"
If File.Exists(StringForTextFile) = True Then
My.Computer.FileSystem.DeleteFile(StringForTextFile)
End If
Dim PerScrnsData As StreamWriter
PerScrnsData = New StreamWriter(StringForTextFile, False, Encoding.Unicode) '
AcceptSettings()
PerScrnsData.Write(PersonalScreensDataToStringAll())
PerScrnsData.Close()
Last edited by EntityX; Nov 25th, 2009 at 02:16 AM.
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
-
Nov 25th, 2009, 02:30 AM
#6
Re: [RESOLVED] Crash when overwriting using StreamWriter.
I don't know the answer to that and I haven't tested it but, on an unrelated note, you should start using the String.Format method:
vb.net Code:
StringForTextFile = String.Format("C:\{0}\{1}Series{2}ScreenNumber{3}.txt", _ Folder4PS, _ IDCodewDashOrN, _ Series, _ ScreenNumber)
That's far easier to read and, therefore, far less error-prone.
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
|