How to set FromFile location in Powershell?

两盒软妹~` 提交于 2019-12-20 07:17:13

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/41458361/how-to-set-fromfile-location-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!