-
May 5th, 2021, 09:27 AM
#1
Thread Starter
PowerPoster
[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.
-
May 5th, 2021, 09:38 AM
#2
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?
-
May 5th, 2021, 12:17 PM
#3
Thread Starter
PowerPoster
Re: Editing a text file
Originally Posted by dday9
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.
-
May 5th, 2021, 01:53 PM
#4
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)
-
May 5th, 2021, 02:54 PM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 5th, 2021, 03:25 PM
#6
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.
-
May 5th, 2021, 05:51 PM
#7
Thread Starter
PowerPoster
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.
-
May 5th, 2021, 09:00 PM
#8
Re: Editing a text file
Originally Posted by Poppa Mintin
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?
-
May 5th, 2021, 09:18 PM
#9
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:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filePath = Path.Combine(Application.StartupPath, "TestData.txt")
Dim fileContent = File.ReadAllText(filePath)
Dim values = fileContent.Split(","c).Select(Function(s) s.Trim(" "c, """"c))
TextBox1.Lines = values.ToArray()
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
Dim filePath = Path.Combine(Application.StartupPath, "TestData.txt")
Dim values = TextBox1.Lines.Select(Function(s) $"""{s}""")
Dim fileContent = String.Join(", ", values)
File.WriteAllText(filePath, fileContent)
End Sub
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
-
May 5th, 2021, 09:25 PM
#10
Re: Editing a text file
Originally Posted by Poppa Mintin
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.
-
May 6th, 2021, 04:08 AM
#11
Thread Starter
PowerPoster
Re: Editing a text file
Originally Posted by jmcilhinney
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.
-
May 6th, 2021, 04:57 AM
#12
Re: Editing a text file
Originally Posted by Poppa Mintin
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?
-
May 6th, 2021, 07:04 AM
#13
Re: Editing a text file
Originally Posted by jmcilhinney
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)
-
May 9th, 2021, 07:43 PM
#14
Thread Starter
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|