如何在Powershell中设置FromFile位置? [英] How to set FromFile location in Powershell?

查看:149
本文介绍了如何在Powershell中设置FromFile位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在准备一个脚本,该脚本需要使用与脚本相同文件夹中的某些图像.这些图像将显示在WinForms GUI上.

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")

当我通过单击从文件夹中手动运行ps1脚本时,它将加载图像并显示它们.不幸的是,我不记得确切的设置方法,但据我所知,它只是用于ps1文件的默认程序. 当我从cmd文件运行脚本(以隐藏cmd窗口)时,它也会加载它们.

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.

但是,当我使用Powershell IDE打开并运行它时,出现错误,并且GUI上没有显示任何图标. 当我使用Powershell打开时,它也无法加载.

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

使用cd $ scriptPath时具有相同的行为,因此当前文件夹很可能不是有罪的.

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

我知道我可以在每个文件读取行(FromFile)中写入$ scriptPath/red.png,但是我要定义一次-FromFile的默认位置-然后不管方式如何都可以进行简单的文件名工作我运行它.

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?

推荐答案

在PowerShell($PWD)中修改默认位置堆栈不会影响主机应用程序的工作目录.

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

要查看实际效果:

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

现在更改位置:

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

当调用带有文件路径参数的.NET方法(如Image.FromFile())时,该路径是相对于后者而不是$PWD解析的.

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.

如果要传递相对于$PWD的文件路径,请执行以下操作:

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

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

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

如果需要相对于执行脚本的路径,则在PowerShell 3.0及更高版本中,可以使用$PSScriptRoot自动变量:

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"    

如果您还需要支持v2.0,则可以在脚本顶部放置以下内容:

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 
}


在交互模式下使用PowerShell时,您可以可以配置prompt函数,使其具有.NET跟随您"的功能,如下所示:


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.

这篇关于如何在Powershell中设置FromFile位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆