Results 1 to 6 of 6

Thread: It refuses to write to a text file if the string used to be a path

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    3

    Unhappy 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!

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    3

    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.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    3

    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:

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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?
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width