-
Jul 30th, 2021, 08:07 AM
#1
Thread Starter
Member
How to make label read text from a text file after publishing?
With this code it changes the label while in visual studio because the path directory is given but after I publish it, the code doesn't seem to work anymore. The label doesn't change even if i cahnge what's written inside. I have already copied the text file into the published folder on my desktop.
Code:
Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'declaring that the label will change based on whatever is written on the text file given that the source path is known
Label8.Text = IO.File.ReadAllText(“C:\Users\THIS-PC\source\repos\chess\bin\Debug\net5.0-windows\quote.txt”)
End Sub
-
Jul 30th, 2021, 09:37 AM
#2
Re: How to make label read text from a text file after publishing?
You should absolutely avoid using hard-coded paths like that. .NET has various options for getting various standard paths for the current environment. In your case, I assume that what you actually want is to read a file from the same folder as the current EXE was run from. In that case, for a WinForms app, use Application.StartupPath:
vb.net Code:
Label8.Text = File.ReadAllText(Path.Combine(Application.StartupPath, “quote.txt”))
On an different note, that you have Form6 and Label8 is terrible. Start providing sensible, descriptive names for all you types, controls, etc. Don't just accept the default names as they are all but meaningless to everyone, including you in a few months of not working on that project.
-
Jul 30th, 2021, 10:19 AM
#3
Thread Starter
Member
Re: How to make label read text from a text file after publishing?
Originally Posted by jmcilhinney
You should absolutely avoid using hard-coded paths like that. .NET has various options for getting various standard paths for the current environment. In your case, I assume that what you actually want is to read a file from the same folder as the current EXE was run from. In that case, for a WinForms app, use Application.StartupPath:
vb.net Code:
Label8.Text = File.ReadAllText(Path.Combine(Application.StartupPath, “quote.txt”))
On an different note, that you have Form6 and Label8 is terrible. Start providing sensible, descriptive names for all you types, controls, etc. Don't just accept the default names as they are all but meaningless to everyone, including you in a few months of not working on that project.
I started using this very recently, like 3-4 days ago which is why I'm making such mistakes. I'll try and fix it, thank you for the advice.
on that note, im getting two errors when using your code stating "file - is not declared" and "Path - is not declared"
-
Jul 30th, 2021, 10:31 AM
#4
Re: How to make label read text from a text file after publishing?
If you want to use a type in code without specifying the namespace then you need to import that namespace. In your original code, you were using IO.File and you were able to do that because the System namespace is imported by default. If you're going to use types in the System.IO namespace multiple times though, you should import that namespace and then use all those types unqualified. If you're going to use the System.IO.File class and the System.IO.Path class then import the System.IO namespace and use both classes unqualified. You can import a namepsace at the top of a code file or, as System already is, you can import it in the project properties so that it applies to all code files.
It may be worth your while to follow the Blog link in my signature below and check out my post on Assemblies & Namespaces.
-
Jul 30th, 2021, 02:01 PM
#5
Re: How to make label read text from a text file after publishing?
To JMcIlhinney's point, the following two are equivalent:
Code:
Imports System ' imported by default, but shown here to demonstrate that it is imported nonetheless
Public Module Module1
Public Sub Main
Dim text = IO.File.ReadAllText(IO.Path.Combine(Application.StartupPath, "quote.txt"))
End Sub
End Module
Code:
Imports System
Imports System.IO
Public Module Module1
Public Sub Main
Dim text = File.ReadAllText(Path.Combine(Application.StartupPath, "quote.txt"))
End Sub
End Module
Notice how in the latter the classes are not prefixed with the IO namespace? That is because the namespace is imported at the top of the code file.
This really boils down to practicality as well as coding style preference. Will you be referencing the System.IO very often (practicality)? Do you want to explicitly show where an object resides every time (coding style preference)?
As a personal matter, if I'm only referencing a namespace a handful of times then I'll just prefix the namespace. However if it starts to become obvious that I'm referencing it a lot, then I'll go ahead and import the namespace.
-
Jul 30th, 2021, 11:49 PM
#6
Re: How to make label read text from a text file after publishing?
Originally Posted by dday9
As a personal matter, if I'm only referencing a namespace a handful of times then I'll just prefix the namespace. However if it starts to become obvious that I'm referencing it a lot, then I'll go ahead and import the namespace.
Indeed, this is a matter of personal preference. My preference is to only refer to a namespace once. If it's used once in code, that will be the one reference. If it's used twice or more, that one reference will be an import.
-
Jul 31st, 2021, 04:16 PM
#7
Re: How to make label read text from a text file after publishing?
BTW... A Label doesn't read anything. Your code assigns the contents of the txt file into a variable, or directly into your Label's Text property.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
|