简体   繁体   中英

How to set FromFile location in Powershell?

I am preparing a script, which needs to use some images from same folder as the script. The images are to be shown on WinForms GUI.

$imgred = [System.Drawing.Image]::FromFile("red.png")

When I run the ps1 script manually from the folder just by clicking, it loads images and shows them. Unfortuantely I do not remember exactly how I set up this, but as far as I can, it was just the default program to use for ps1 files. When I run the script from a cmd file (to hide the cmd window), it also loads them.

But when I open with Powershell IDE and run it, I get errors and no icons are shown on my GUI. When I open with Powershell it also fails to load them.

The only difference between those run modes I can find is with:

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$scriptPath             #always same, the location of script
(Get-Location).Path     #scriptlocation when icons loaded, system32 folder when unsuccessful load

Same behavior when doing cd $scriptPath, so the current folder is most likely not the guilty one.

I know I can write $scriptPath/red.png in each file read line (FromFile), but what I want is to define it once - the default location for FromFile - and then just have simple filename work regardless of the way I run it.

What is to be changed so the default file reading path is same as my scripts location?

Modifying the default location stack in PowerShell ( $PWD ) doesn't affect the working directory of the host application.

To see this in action:

PS C:\Users\Mathias> $PWD.Path
C:\Users\Mathias
PS C:\Users\Mathias> [System.IO.Directory]::GetCurrentDirectory()
C:\Users\Mathias

now change location:

PS C:\Users\Mathias> cd C:\
PS C:\> $PWD.Path
C:\
PS C:\> [System.IO.Directory]::GetCurrentDirectory()
C:\Users\Mathias

When you invoke a .NET method that takes a file path argument, like Image.FromFile() , the path is resolved relative to the latter, not $PWD .

If you want to pass a file path relative to $PWD , do:

$pngPath = Join-Path $PWD "red.png"
[System.Drawing.Image]::FromFile($pngPath)

or

[System.Drawing.Image]::FromFile("$PWD\red.png")

If you require a path relative to the executing script, in PowerShell 3.0 and newer you can use the $PSScriptRoot automatic variable:

$pngPath = Join-Path $PSScriptRoot "red.png"    

If you need to support v2.0 as well, you could put something like the following at the top of your script:

if(-not(Get-Variable -Name PSScriptRoot)){
  $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Definition -Parent 
}

When using PowerShell in interactive mode, you could configure the prompt function to have .NET "follow you around" like so:

$function:prompt = {
    if($ExecutionContext.SessionState.Drive.Current.Provider.Name -eq "FileSystem"){
        [System.IO.Directory]::SetCurrentDirectory($PWD.Path)
    }
    "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}

but I would recommend against that, just get into the habit of providing fully qualified paths instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM