Results 1 to 14 of 14

Thread: [RESOLVED] Editing a text file

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Resolved [RESOLVED] Editing a text file

    Hi,

    I've been trying to implement these instructions from John Jcilhinney for a few days now...
    Re: Edit a text file.
    The whole point of resources is that they are not separate files, but data compiled into the assembly itself. They exist specifically so that they can't be changed or deleted. If you want to edit a file then you should be using a resource in the first place, because there is no file specifically so there is no editing.

    You should be adding a text file to your project in the Solution Explorer and then making sure that its Build Action is Content and Copy to Output Directory is set to Copy if Newer. If you use Copy Always then any changes you make at run time will be overwritten the next time you build, just as happens with database files like MDBs or ACCDBs.
    ...and getting more bogged down as the days go by.

    I suspect that in the line "If you want to edit a file then you should be using a resource in the first place", the 'should be using' ought to be 'shouldn't be using'. So I've been trying to follow the second paragraph but sadly getting nowhere.

    I can add files via the Solution Explorer and ensure that their Build Action is 'Content' and Copy to Output Directory is set to 'Copy if Newer'.
    I've added them as .txt files where their text is in this form...
    "Clwyd", "Congo", "Conwy", "Corby", "Corfu", "Corse", "Cowes", "Crewe"
    ...hopefully ready to be 'Split' into data in a List(Of String) but can't find how to read 'em. I'm even struggling to find how address 'em... I don't believe this works...
    Code:
    Dim filePath As String = IO.Path.Combine(Application.StartupPath, fileName)    ' No extension.
    ...because I can see the files in File Explorer and open 'em as though they were a Form. I'm not even sure that I have to read 'em in, they might be there already.

    I'm sure that using the old 'FileOpen' and 'LineInput' would be frowned upon today so I've tried to use a StreamReader but not sure how to do that. I've got working examples in other projects but haven't been able to adapt 'em. I suspect I'm getting the syntax wrong, intellisense isn't helping... it's saying I'm wrong but isn't helping. I don't believe the file path is helping.

    A little help would be much appreciated.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Editing a text file

    What is wrong? Is it that your filePath variable is referencing the wrong path name? Or is it that the path name is correct, but you don't know how to read the file?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Editing a text file

    Quote Originally Posted by dday9 View Post
    What is wrong? Is it that your filePath variable is referencing the wrong path name? Or is it that the path name is correct, but you don't know how to read the file?
    I'm sorry Dday9, it could be both of those or either... I'm floundering here.
    Along with the sunshine there has to be a little rain sometime.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Editing a text file

    You're not giving me much to go off of.

    Assuming your filename is correct, which it may not be, you would use IO.File.ReadAllText (documentation):
    Code:
    Dim contents = IO.File.ReadAllText(filePath)
    To split the text, you would use String.Split (documentation):
    Code:
    Dim values = contents.Split(","c, StringSplitOptions.RemoveEmptyEntries)
    Make the changes you want to the values. Once you want to join them back again, use String.Join (documentation):
    Code:
    contents = String.Join(", ", values)
    To write back to the file use IO.File.WriteAllText (documentation):
    Code:
    IO.File.WriteAllText(filePath, contents)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Editing a text file

    You can read from My.Resources at runtime, but you can't write to it. For filesystem files, i'd use IO.File.ReadAllLines and IO.File.WriteAllLines

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Editing a text file

    An alternative would be to store your collection as a setting. I'd be more inclined to suggest that over storing the values in a text file.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Editing a text file

    Thanks guys, it's the implementing of John's second paragraph that I trying to accomplish.

    The relevant project has been working for a good while, but the (six) text files are in in separate modules where I can just read them directly. Adding a word or two here and there is a matter of opening the text in a module with Notepad, modifying it and saving back in the module, that's much faster than opening them in SV and waiting for 'Line commit' to complete before I can do anything, especially with a file.length currently of 94,934 words.
    But then I have to open the project in VS, and run it (just once), and close it again after which I can run the .exe file and the new additions will work. Just adding to the module without running in SV doesn't add the new words. Which means if I were to give it to (say) my grandson, he wouldn't be able to add any words he might find missing.

    So, from what John says I ought to be able to add words to the text files and just run the .exe file to have any new additions included. Maybe I've misunderstood that too.

    I'm struggling to read the files.

    Poppa
    Along with the sunshine there has to be a little rain sometime.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Editing a text file

    Quote Originally Posted by Poppa Mintin View Post
    I suspect that in the line "If you want to edit a file then you should be using a resource in the first place", the 'should be using' ought to be 'shouldn't be using'.
    You are correct. Do you have a link to the thread I posted that in, so that I can edit it?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Editing a text file

    I just created a Windows Forms Application project targeting .NET Framework 4.8. I added a TextBox to the form and set Multiline to True. I added a text file named TestData.txt to the project and set Copy to Output Directory to Copy if Newer. I copied the text from post #1 into that file. I then added this code to my form:
    vb.net Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    6.         Dim filePath = Path.Combine(Application.StartupPath, "TestData.txt")
    7.         Dim fileContent = File.ReadAllText(filePath)
    8.         Dim values = fileContent.Split(","c).Select(Function(s) s.Trim(" "c, """"c))
    9.  
    10.         TextBox1.Lines = values.ToArray()
    11.     End Sub
    12.  
    13.     Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    14.         Dim filePath = Path.Combine(Application.StartupPath, "TestData.txt")
    15.         Dim values = TextBox1.Lines.Select(Function(s) $"""{s}""")
    16.         Dim fileContent = String.Join(", ", values)
    17.  
    18.         File.WriteAllText(filePath, fileContent)
    19.     End Sub
    20.  
    21. End Class
    I ran the project and I saw the quoted values in the file displayed on separate lines in the TextBox. I added a couple of values and quit the app. When I ran the project again, I saw those added values in the TextBox again, along with the original values. QED

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Editing a text file

    Quote Originally Posted by Poppa Mintin View Post
    it could be both of those or either... I'm floundering here.
    That's why you need to take note of the error message and/or symptoms. That will lead you to the cause. For instance, if you get a FileNotFoundException then the issue is obviously not that you can't read the data from the file because you haven't even found the file. This:
    Code:
    im filePath As String = IO.Path.Combine(Application.StartupPath, fileName)    ' No extension.
    indicates that you're not using the correct file path. If the file name has an extension, which it should, then that is part of the path. If you added that file to your project resources then the resource generated would be named after the file without the extension but you're not using a resource, so that's not relevant here. If you provide a FULL and CLEAR explanation of the problem, i.e. what you're trying to achieve, how you're trying to achieve AND what happens when you try, then the diagnosis process is much easier. The actual behaviour of the application is ALWAYS relevant.

  11. #11

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Editing a text file

    Quote Originally Posted by jmcilhinney View Post
    You are correct. Do you have a link to the thread I posted that in, so that I can edit it?
    It ought to be here
    Along with the sunshine there has to be a little rain sometime.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Editing a text file

    Quote Originally Posted by Poppa Mintin View Post
    It ought to be here
    Thanks. Fixed. That's not the first time I've made a typo like that. How much trouble could it cause to post the exact opposite to what I meant to say?

  13. #13
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Editing a text file

    Quote Originally Posted by jmcilhinney View Post
    Thanks. Fixed. That's not the first time I've made a typo like that. How much trouble could it cause to post the exact opposite to what I meant to say?
    It is also a good way to : know that people read you, make them think by themself and discover the typo .

    only people who does nothing and don't help don't make mistake...
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  14. #14

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Editing a text file

    Thanks for all the help guys...

    Finally got my app. saving new entries and using 'em A. Straight away, and B. Next time I run the app.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

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