Author Topic: Store file path in application  (Read 1682 times)

ASF

  • Newbie
  • *
  • Posts: 26
    • View Profile
Store file path in application
« on: October 05, 2017, 06:52:06 PM »
Hi all,

I have an AHMI application which runs some external scripts from a folder, and logs to some CSV files in another folder. Because the application is running on a Compute Stick (very lightweight PC), I have to do the development on a different machine and then copy the files across.

When I want to test my changes, I just plug in the development PC and get my changes debugged, and then copy over to the runtime PC. The only problem is, if I want to make changes to the data logging or script calling, I can't do that, because the file structure is different on the development PC to the runtime PC, so all my code points to non-existent directories.

What I'd like to do is have the code more generic, and have it retrieve the required filepath from a settings screen, so that when I run it up on the development machine to test, I just have to change the filepath on the settings screen, and it will run fine. Then when I transfer it to the runtime PC, I change it back.

I have no problems with using VB to call the file select dialog to select my filepath, and I can probably then integrate that filepath into my code without too much trouble - but what I don't know is how the selected filepath can be "saved" within the application, so that if the PC gets rebooted, it's still got that filepath saved somewhere, and I don't have to re-select it every time the PC boots up.

Is there a way to do this?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Store file path in application
« Reply #1 on: October 05, 2017, 07:16:41 PM »
Maybe Application Settings will do what you want. See this thread that explains how to use them. Although that post explains its use in a driver, it should work for any property of any component:

http://advancedhmi.com/forum/index.php?topic=1797.msg9960#msg9960

ASF

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Store file path in application
« Reply #2 on: October 05, 2017, 08:32:37 PM »
That looks like what I'm after. It's not quite working, but I'm probably just missing one thing.

I'm using the FolderBrowserDialog object to select the file path. I have created an application setting called SelectedFilePath, and bound the SelectedPath property of FolderBrowserDialog1 to the SelectedFilePath application setting.

To do a simple test, I've set up two buttons and a label:

Select File Path Button - when clicked, runs the command "FolderBrowserDialog1.ShowDialog()". This brings up the path select dialog.

Check File Path Button - when clicked, runs the command "BasicLabel1.Value = My.Settings.SelectedFilePath"

So when I click the first button, I can select a file path. When I click the second, it should show me the filepath on the label. It doesn't work; it just shows me the default selected path of the FolderBrowserDialog1

If I change the command on the second button to "BasicLabel1.Value = FolderBrowserDialog1.SelectedPath", it works - I can use the first button to select a file path, and then if I click the second button, it shows the path I just selected. So it seems that the folder browser dialog has recorded the selected path and is "remembering" it just fine - but it's not updating the application setting. The application setting just sees the default "SelectedPath" value.

What am I missing?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Store file path in application
« Reply #3 on: October 05, 2017, 08:43:12 PM »
With the BasicLabel selecting, Using the Properties Window go to the Application Setting and bind the Value property to the SelectedFilePath setting.

you now then want to change the value like this:

My.Settings.SelectFilePath = FolderBrowserDialog1.SelectedPath

Through the binding, the BasicLabel will update automatically. Also when you exit the application normally, it will save the latest value,

Phrog30

  • Guest
Re: Store file path in application
« Reply #4 on: October 05, 2017, 09:04:40 PM »
Just be aware that it only saves on a normal shutdown, so if you have a crash it probably won't save. That was a deal breaker for me. I prefer to use an ini file for global application settings.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Store file path in application
« Reply #5 on: October 05, 2017, 09:26:26 PM »
Just be aware that it only saves on a normal shutdown, so if you have a crash it probably won't save.
You can get around this by calling

My.Settings.Save()

after any value changes

ASF

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Store file path in application
« Reply #6 on: October 05, 2017, 09:45:35 PM »
I can't get that to work for me - I get the error 'Property "SelectedFilePath" is read-only'

Good to know about the saving settings though - I'll make sure to integrate that

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Store file path in application
« Reply #7 on: October 05, 2017, 10:23:11 PM »
- In Solution Explorer right the AdvancedHMI project and select Properties
- Select the Settings tab
- See if the Scope of your SelectPath is User
« Last Edit: October 05, 2017, 11:03:26 PM by Archie »

ASF

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Store file path in application
« Reply #8 on: October 05, 2017, 10:29:54 PM »
Ah, no I changed it from User to Application. I'll change it back and try again.

What's the difference between the two? I assumed that it was a case of "this setting is specific to the currently logged in Windows user" vs "this setting is the same across the whole application, no matter who is logged in". I don't want each user to have to set this up each time if possible.