Results 1 to 16 of 16

Thread: [RESOLVED] Edit 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] Edit a text file.

    Hi,

    I have a little test application that I am testing for a possibility of changing a file within my resources.
    Here is my code:
    Code:
    Public Class Form1
    
        Private Sub Form1_Load() Handles MyBase.Load
            Dim lizt(), txt As String
    
            Label1.Dock = DockStyle.Fill
            Label1.Text = Nothing
            lizt = My.Resources.ResourceManager.GetString("Dummy").Split _
                              ({Environment.NewLine}, StringSplitOptions.None)
            For i = 0 To lizt.Count - 1
                txt = lizt(i)
                Label1.Text &= txt & vbCrLf
            Next
            Button1.BringToFront()
        End Sub
    
        Private Sub Button1_Click() Handles Button1.Click
            Me.Close()
        End Sub
    
    End Class
    I think this should read from the file 'Dummy.txt' which is in my resources, and should display what it finds there in Label1, which it does when I'm in debug mode.
    The app. also appears to do that when I launch the app. in the normal way.

    Now... If I now open the file that's in my resources by navigating to it in File Explorer and double-clicking it in the usual way, I can exit the text, close the file with 'save'. Opening the file again confirms that the file in resources has been edited.

    Now when I launch the app. I'd expect to see the edited file, but in fact it shows the original text. However, if I open the app. with VS2019 in debug mode and click 'Start' the app. runs and does show the edited text. Thereafter running in usual way it shows the edited text.

    What is happening here ?


    Poppa
    Last edited by Poppa Mintin; May 5th, 2021 at 07:26 AM.
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Edit a text file.

    Hi.
    Try moving the file in another folder than the resources and test again.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Edit a text file.

    The file in your resources folder is not what you are accessing when you run code. The file gets embedded in the exe and you access it via the resource manager. If you want to do this, your best bet is to select the resource in VS, then set the Copy to Output Directory property to Copy Always or Copy if Newer. A copy of the resource will then be placed in the output folder (i.e bin\debug, bin\release) where you can open the file directly. That said, I don't think you can save it back to the same path without elevated privileges'.

    We do this type of thing by saving a copy of the file in the AppDataPath (if it doesn't exists) when the app launches. We are then free to open the copy in the AppDataPath and save it as needed without issue.
    kevin

    edit:
    I think I misunderstood just a bit. I first thought, that you were trying to edit and save the resource file from from the running app. Part of my reply therefore probably does not apply. The basic premise is still there though.
    Last edited by kebo; Nov 10th, 2020 at 11:03 AM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Edit a text file.

    Quote Originally Posted by Poppa Mintin View Post
    Now when I launch the app. I'd expect to see the edited file, but in fact it shows the original text. However, if I open the app. with VS2019 in debug mode and click 'Start' the app. runs and does show the edited text. Thereafter running in usual way it shows the edited text.

    What is happening here ?
    Resources are put into the executable when you compile, so your app will contain the resources at the time you compiled that particular version of the app.

    Clicking "start" re-compiles, and adds the resources that are current at that time, so you get the edited version.


    If you want to include a file that can be edited after compiling, you will need to store it (or just the edited version) somewhere other than resources.


    edit: beaten to it!

  5. #5
    New Member
    Join Date
    Nov 2020
    Posts
    3

    Re: Edit a text file.

    I would just read the file with
    Code:
    IO.File.ReadAllText("the path")

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

    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 shouldn't 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.
    Last edited by jmcilhinney; May 6th, 2021 at 04:56 AM. Reason: Fixed typo to change "should" to "shouldn't".

  7. #7

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

    Re: Edit a text file.

    Quote Originally Posted by jmcilhinney View Post
    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.
    Thanks John,

    I've been trying to do that, I've just opened the Solution Explorer and 'dropped' a text file in there.
    It went in ok and was sorted so I'm guessing that means it's in and accepted.
    When I right-click it and select 'Properties' the properties dialogue opens but it's totally empty except for three buttons: -
    Categorized, Alphabetical and Property Pages. None of these buttons appear actually do anything.

    I'm assuming you didn't mean to add the file in the resources because that's what you're specifically saying.


    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,297

    Re: Edit a text file.

    Generally speaking, I would add a text file to a project exactly as I would any other item. I would right-click the project, select Add -> New Item and then select Text File as the item template to create a new file, or I would right-click the project and select Add -> Existing Item and then select an existing text file. You can then just click the item in the Solution Explorer to have the Properties window populated with the properties for that item.

  9. #9

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

    Re: Edit a text file.

    Thanks John,

    I figured that out and I now have two files in Solution Explorer, both set: -

    Build Action = Content
    Copy to Output Directory = Copy if Newer.

    But I've spent over three hours scouring the web, trying to discover how to read from these files.
    I just can not find how to address the files to read from them.

    Do I have to 'read 'em in' every time I open the app.? 'Copy if Newer' seems to imply not.
    Do I have to read them at all before I use 'em? are they automatically 'read in' as part of the project?

    I want to use 'em as a source of data to be used in List(Of String)s. (Lists of string ?)
    I intend to use perhaps as many as ten such files and would like to be able to populate the List(Of String)s from them.

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

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

    Re: Edit a text file.

    Quote Originally Posted by Poppa Mintin View Post
    Do I have to 'read 'em in' every time I open the app.? 'Copy if Newer' seems to imply not.
    It doesn't imply that at all. You have to read the file regardless. If you use Copy Always then the source file will be copied from your project folder to the output folder every time the EXE is built. If you use Copy if Newer then the source file be copied when the EXE is built if and only if there is no file already in the output folder or the file already there is newer than the source file. That means that you can make changes at run time when testing and not have those changes overwritten next time you build. The file in the output folder will only be overwritten if you make a change to the source and then build. If you use Never Copy then the source file not copied and you have to put it in the output folder or a different location yourself.

    Assuming that the file is copied to the output folder, it will be in the same folder as your EXE. That folder path can be found using Application.StartupPath in code. That means that, if you have a file named MyFile.txt and you want to read the lines into a List(Of String) then you could do it like this:
    vb.net Code:
    1. Dim myList As New List(Of String)
    2.  
    3. myList.AddRange(File.ReadAllLines(Path.Combine(Application.StartupPath, "MyFile.txt")))
    or:
    vb.net Code:
    1. Dim myList = File.ReadAllLines(Path.Combine(Application.StartupPath, "MyFile.txt")).ToList()

  11. #11

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

    Re: Edit a text file.

    Ok...

    I'm getting there.
    I have ten files installed, I have ten List(Of String) all populated (I discovered that I need 'Imports System.IO' to make it work.)
    But I've used (what I consider to be) a clumsy way to go about it... i.e. Select Case with ten Cases.
    vb.net Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Imports System.IO
    5.  
    6. Public Class Form1
    7.  
    8.     'Public DataFile0, DataFile1, DataFile2, DataFile3, DataFile4 As New List(Of String)
    9.     'Public DataFile5, DataFile6, DataFile7, DataFile8, DataFile9 As New List(Of String)
    10.  
    11.     Dim Fylez() As String = {"sfFolkez", "sfGoodBookz", "sfLFCz", "sfMythz", "sfNamez",
    12.                              "sfPopStarz", "sfSpotz", "sfUKPMz", "sfUSAprez", "sfWordz"}
    13.  
    14.     Private Sub Form1_Load() Handles MyBase.Load
    15.         Dim fyle, txt As String, dataIn As New List(Of String)
    16.  
    17.         For a = 0 To 9
    18.             fyle = Fylez(a) & ".txt"
    19.             dataIn.Clear()
    20.             dataIn.AddRange(File.ReadAllLines(Path.Combine(Application.StartupPath, fyle)))
    21.             dataIn.Sort()
    22.         Select Case a
    23.             Case = 0
    24.                 For b = 0 To dataIn.Count - 1
    25.                     txt = dataIn(b)
    26.                     DataFile0.Add(txt)
    27.                 Next
    28.             Case = 1
    29.                 For b = 0 To dataIn.Count - 1
    30.                     txt = dataIn(b)
    31.                     DataFile1.Add(txt)
    32.                 Next
    33.             Case = 2
    34.                 For b = 0 To dataIn.Count - 1
    35.                     txt = dataIn(b)
    36.                     DataFile2.Add(txt)
    37.                 Next
    38.             Case = 3
    39.                 For b = 0 To dataIn.Count - 1
    40.                     txt = dataIn(b)
    41.                     DataFile3.Add(txt)
    42.                 Next
    43.             Case = 4
    44.                 For b = 0 To dataIn.Count - 1
    45.                     txt = dataIn(b)
    46.                     DataFile4.Add(txt)
    47.                 Next
    48.             Case = 5
    49.                 For b = 0 To dataIn.Count - 1
    50.                     txt = dataIn(b)
    51.                     DataFile5.Add(txt)
    52.                 Next
    53.             Case = 6
    54.                 For b = 0 To dataIn.Count - 1
    55.                     txt = dataIn(b)
    56.                     DataFile6.Add(txt)
    57.                 Next
    58.             Case = 7
    59.                 For b = 0 To dataIn.Count - 1
    60.                     txt = dataIn(b)
    61.                     DataFile7.Add(txt)
    62.                 Next
    63.             Case = 8
    64.                 For b = 0 To dataIn.Count - 1
    65.                     txt = dataIn(b)
    66.                     DataFile8.Add(txt)
    67.                 Next
    68.             Case = 9
    69.                 For b = 0 To dataIn.Count - 1
    70.                     txt = dataIn(b)
    71.                     DataFile9.Add(txt)
    72.                 Next
    73.         End Select
    74.     End Sub
    I'm trying to do that more tidily with a loop using an array of List(Of String) in stead of ten separate lists. : -
    vb.net Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Imports System.IO
    5.  
    6. Public Class Form1
    7.  
    8.     Public DataFile(9) As List(Of String)
    9.     Dim Fylez() As String = {"sfFolkez", "sfGoodBookz", "sfLFCz", "sfMythz", "sfNamez",
    10.                              "sfPopStarz", "sfSpotz", "sfUKPMz", "sfUSAprez", "sfWordz"}
    11.  
    12.     Private Sub Form1_Load() Handles MyBase.Load
    13.         Dim fyle, txt As String, dataIn As New List(Of String)
    14.  
    15.         For a = 0 To 9
    16.             fyle = Fylez(a) & ".txt"
    17.             dataIn.Clear()
    18.             dataIn.AddRange(File.ReadAllLines(Path.Combine(Application.StartupPath, fyle)))
    19.             dataIn.Sort()
    20.             For b = 0 To dataIn.Count - 1
    21.                 txt = dataIn(b)
    22.                 DataFile(a).Add(txt)
    23.             Next
    24.         Next
    But I have a problem with that...
    At Line 22 in the loop, I get an error message: -

    System.NullReferenceException
    HResult=0x80004003
    Message=Object reference not set to an instance of an object.
    Source=Single file
    StackTrace:
    at Single_file.Form1.Form1_Load() in D:\~VB Trials\Single file\Form1.vb:line 22
    And I can't find a way around that.

    I've run into problems with List(Of String) before when I've not used 'Dim DataFile As NEW List(Of String)' but intellisence won't allow the 'New' anywhere in my code above. I guess that's because I'm trying to make an array of List(Of String) because I hope to be able to address an array more easily than separates later in the project.


    Poppa
    Last edited by Poppa Mintin; Nov 19th, 2020 at 12:45 PM.
    Along with the sunshine there has to be a little rain sometime.

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

    Re: Edit a text file.

    the error message is clear you need to create an instance of your variable datafile, so you need to use the new method somewhere

    To my knowledge, you cannot limit the size of a list (at least not directly) so you will need to have either

    Code:
    Public DataFile As new List(Of String)
    or
    Code:
    Public DataFile(9) As  String
    if you want to keep the array, you just need to replace

    Code:
    DataFile(a).Add(txt)
    by
    Code:
    DataFile(a)=txt
    Last edited by Delaney; Nov 19th, 2020 at 01:05 PM.
    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)

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Edit a text file.

    You've mi-interpreted there, the array is of multiple List(of String)

    The issue is that those lists need to be initialised (and can't be in the declaration line), but thankfully it isn't too hard to do. For a single list you can use the shorthand:
    Code:
    Public X As New List(Of String)
    ...or you could use the long-winded way instead, by spreading it over two lines:
    Code:
    Public X As List(Of String)
    X = New List(Of String)
    As you can't use the shorthand this time, you need to use the long-winded way, eg:
    Code:
                dataIn.Sort()
                DataFile(a) = New List(Of String)
                For b = 0 To dataIn.Count - 1

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

    Re: Edit a text file.

    Quote Originally Posted by si_the_geek View Post
    You've mi-interpreted there, the array is of multiple List(of String)
    Yes, too quick to read and answer : the best way to do a 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)

  15. #15

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

    Re: Edit a text file.

    Thanks Si_the_geek,

    That done it, much neater, thank you.

    All I have to do now is figure out how to read from these List(Of String)




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

  16. #16

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

    Re: Edit a text file.

    Thanks guys...

    All done.


    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