It refuses to write to a text file if the string used to be a path
Hi!
I've searched everywhere to find out why the heck my program is doing this, but couldn't find anything like it...?
I'm making a flash card program. Part of my program takes images corresponding to other data from a certain area, chosen by the user, and moves them to another folder, for later use. I want to store the path for the image next to the other data, storing the path as a string. When no image is chosen, the variable for the path is simply "no image".
For some reason, whenever the variable for the path contains an actual path, the program won't write it, but will when the variable is "no image".
This is the code where the user selects an image:
Code:
Private Sub btnImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImage.Click
imgBox.Width = 325
imgBox.Height = 350
opnImage.ShowDialog()
imagePath = opnImage.FileName
If imagePath <> "OpenFileDialog1" Then
n += 1
Dim fileExtension As String = Path.GetExtension(imagePath)
newName = n & fileExtension
newLocation = imgDirectory & newName
If File.Exists(newLocation) Then
File.Delete(newLocation)
End If
File.Copy(imagePath, newLocation)
imgBox.Image = Image.FromFile(newLocation)
ResizePicture()
End If
End Sub
This is the code for writing the information to the text file:
Code:
Sub WriteLine()
Dim courseFile As StreamWriter = File.AppendText(currentCourse & ".txt")
Dim newLine As String = Question & "@" & Answer & "@" & newLocation
courseFile.WriteLine(newLine)
End Sub
I have tried putting the path into a text box first, and then writing it from there, and it won't write. I've tried creating substrings from the path, and then putting them together, and it refuses. I am completely confused!
Re: It refuses to write to a text file if the string used to be a path
So which variable are you talking about? Is it the one named "currentCourse" ? If so then where and how are you setting the value of this variable? Not quite sure what you are talking about with the substrings but if you just mean doing that to construct a valid path then you should be using IO.Path.Combine instead.
Re: It refuses to write to a text file if the string used to be a path
When the variable newLocation used to be a path, it doesn't write the line containing "question & answer & newLocation" at all.
as for the substrings, I was just checking if the program was being buggy because the variable used to be a path, so I tried to "hide" it in different ways to see if it changed things (it didn't). It doesn't write even a part of the path; if the line contains what used to be the path, it doesn't write at all.
Re: It refuses to write to a text file if the string used to be a path
Cool. How did you try to hide it? Did you look at the characters in the string? Try replacing any slashes with something else and see what that does.
Re: It refuses to write to a text file if the string used to be a path
Okay, I tried replacing the slashes, which sounds like a good idea...
But for some reason, now it won't write at all. Anything; I go through the steps and it acts like it writes the line but doesn't, and doesn't give any error messages. I thought that maybe I was checking the wrong folder, but it is in the right one. I tried re-writing the code, changing the file name, changing the variable, removing a variable and using a straight string, and nothing works..
Could this be related at all?
.. I think I'm just going to try re-writing the program from scratch... D:
Re: It refuses to write to a text file if the string used to be a path
I haven't done anything like that for a long time. Is there any buffer to flush on the StreamWriter?
If you re-write the program from scratch and it still doesn't work, then what will you do? I would be inclined to study this problem until you figure out what is happening. How about posting the code as it is now with the changes you made?