Results 1 to 7 of 7

Thread: How to make label read text from a text file after publishing?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    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

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

    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:
    1. 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.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: How to make label read text from a text file after publishing?

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. 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"

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

    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.

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

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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

    Re: How to make label read text from a text file after publishing?

    Quote Originally Posted by dday9 View Post
    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.

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

    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.

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