How to reference files included in a Forms Project

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
McKenning
Posts: 13
Last visit: Tue Aug 18, 2020 11:13 am

Re: How to reference files included in a Forms Project

Post by McKenning »

Okay, so just so I understand, if I use an exe, any text files added to the project will be discarded. If I build an MSI, any text files included in the project will be available and copied to the installation location. Is that correct?

What if the contents of the text file is a powershell script? Can I access the contents in the exe? If I can call it, can I redirect the contents to a text file on the local filesystem?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to reference files included in a Forms Project

Post by jvierra »

You can use the properties of the added file (in the project) to include the file in the build.
stuff.png
stuff.png (27.57 KiB) Viewed 3750 times
User avatar
McKenning
Posts: 13
Last visit: Tue Aug 18, 2020 11:13 am

Re: How to reference files included in a Forms Project

Post by McKenning »

That's very helpful. So when I reference this file, I use Call-filename_txt. What output does this call give? If I set it to a variable, will the variable contain the contents of the file? Like this:
  1. $textfile = Call-filename_txt
Or is there another way to do this?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to reference files included in a Forms Project

Post by jvierra »

Sorry but the references is not usable. You just need to know that the file will be placed in the installation folder with the exe.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: How to reference files included in a Forms Project

Post by dan.potter »

McKenning wrote:That's very helpful. So when I reference this file, I use Call-filename_txt. What output does this call give? If I set it to a variable, will the variable contain the contents of the file? Like this:
  1. $textfile = Call-filename_txt
Or is there another way to do this?

Can you give an example of what you are trying to do? What would the text file contain? Are you trying to get contents of a text file?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to reference files included in a Forms Project

Post by jvierra »

Here is how to find your file:
  1. $filepath= Join-Path $myinvocation.PSScriptRoot $myfile
User avatar
McKenning
Posts: 13
Last visit: Tue Aug 18, 2020 11:13 am

Re: How to reference files included in a Forms Project

Post by McKenning »

dan.potter wrote: Can you give an example of what you are trying to do? What would the text file contain? Are you trying to get contents of a text file?
Yes, I am trying to get the contents of the text file so that I can update a script that will be called remotely by this project. So I want to do something like this:
  • 1. Click a button to call the remote script.
    2. The code in the button will get the contents of the bundled text file and copy it to a file in the filesystem of the executing machine.
    3. The code in the button will then copy the file to the filesystem of the remote machine.
    4. The code in the button will then call that remote file as a script and have it execute.
    5. The output of the remote script will be displayed on the screen for the user.
I already have all of this working with the exception of #2. I can copy an existing file to the remote machine, but I'd like there to be a way for the form to update the file automatically when there are revisions. The goal is to make this as automated for the end-user as possible.
User avatar
McKenning
Posts: 13
Last visit: Tue Aug 18, 2020 11:13 am

Re: How to reference files included in a Forms Project

Post by McKenning »

jvierra wrote:Here is how to find your file:
  1. $filepath= Join-Path $myinvocation.PSScriptRoot $myfile
Okay, so the txt file will be dropped when the exe runs and I can see the file at the location above? What is the $myfile variable?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to reference files included in a Forms Project

Post by jvierra »

Include the script as content. Reference it as I posted above:
  1. filepath= Join-Path $myinvocation.PSScriptRoot $myfile
  2. Invoke-Command -FilePath $myfile -Computer <remotepc>
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to reference files included in a Forms Project

Post by jvierra »

McKenning wrote:
jvierra wrote:Here is how to find your file:
  1. $filepath= Join-Path $myinvocation.PSScriptRoot $myfile
Okay, so the txt file will be dropped when the exe runs and I can see the file at the location above? What is the $myfile variable?
No - the file will be installed along with the EXE by the installer. It will be in the installation folder
This topic is 7 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked