我有一个自定义 cmdlet,可以像这样调用:
Get-Info ".\somefile.txt"
我的命令行开关代码如下所示:
[Parameter(Mandatory = true, Position = 0)]
public string FilePath { get; set; }
protected override void ProcessRecord()
{
using (var stream = File.Open(FilePath))
{
// Do work
}
}
但是,当我运行命令时,出现此错误:
找不到文件“C:\Users\Philip\somefile.txt”
我不是从 C:\Users\Philip
执行此 cmdlet。由于某种原因,我的 cmdlet 没有检测到工作目录,因此这样的本地文件不起作用。在 C# 中,当提供本地“.\”文件路径时,检测正确文件路径的推荐方法是什么?
最佳答案
查看 SessionState 属性的 Path 属性。它有一些常用来解析相对路径的实用函数。选择会有所不同,具体取决于您是否要支持通配符。这个forum post可能有用。
关于c# - Powershell Cmdlet : Determine actual filepath of file in local directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9914173/