Author Topic: Setting File Path  (Read 1183 times)

ddddd13

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Setting File Path
« on: June 13, 2019, 08:57:03 AM »
I am trying to read a file from the hard drive using the following.

    Dim FILE_NAME As String = "C:\Files\Parameter.txt"
        Using sr As New System.IO.StreamReader(FILE_NAME)

It does not work. I have searched on line and found many different examples using things that just don't make sense yet none of them work.

What's your suggestion?

Dave

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5266
    • View Profile
    • AdvancedHMI
Re: Setting File Path
« Reply #1 on: June 13, 2019, 09:15:16 AM »
What error do you get? Is it possibly a permission thing since it is a directory in the root of the C drive?

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: Setting File Path
« Reply #2 on: June 13, 2019, 09:28:51 AM »
After making sure the path is completely correct, try with a different path. One that you are sure it has no permission or system protection, like your "My Documents" folder or even a test with a USB drive. It depends on your IT policies for file access.

ddddd13

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Setting File Path
« Reply #3 on: June 13, 2019, 09:51:15 AM »
    The following is all the code.

    Dim FILE_NAME2 As String = "C:\Files\Files\PARAMETER.txt"
        Using sr As New System.IO.StreamReader(FILE_NAME2)
            While Not sr.EndOfStream
                Parameter.Add(sr.ReadLine())
            End While
        End Using
        RichTextBox2.Text = Parameter(0) & Parameter(1) & Parameter(2)

Notice I added another directory under the Files directory. The file exists yet it is not being read. If I change the file name to one that does not exist it operates the same. I even tried changing the file name to PARAMETER.TXT.TXT
Dave

ddddd13

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Setting File Path
« Reply #4 on: June 13, 2019, 01:48:48 PM »
This code was under form active. For some reason it was not running. I have no idea why. Everything else worked, and now the code is running and it works.

??????
Dave

dmroeder

  • Global Moderator
  • Full Member
  • *****
  • Posts: 208
    • View Profile
Re: Setting File Path
« Reply #5 on: April 07, 2022, 10:20:00 AM »
It's strange that for me, it works on my pc, but it doesn't on my laptop. Not really sure what to do.

Welcome to the forum necroposter!

My guess is that this is a permission problem, as was suggested in earlier posts.  Try pointing to your users documents folder rather than the C drive directly.  Rather than hard code the path to the documents folder, the .NET framework will get that path for you, so you don't have to worry about different users being able to run your program:

Code: [Select]
Dim FILE_NAME As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\PARAMETER.txt"