【发布时间】:2012-04-12 10:31:30
【问题描述】:
我有一个可以这样调用的自定义 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
}
}
但是当我运行命令时,我得到了这个错误:
Could not find file 'C:\Users\Philip\somefile.txt'
我没有从 C:\Users\Philip 执行此 cmdlet。由于某种原因,我的 cmdlet 没有检测到工作目录,所以像这样的本地文件不起作用。在 C# 中,当提供本地“.\”文件路径时,推荐的检测正确文件路径的方法是什么?
【问题讨论】:
-
是的,我有,结果是 'C:\Users\Philip\'
-
这显然是设计使然:windowsitpro.com/article/windows-powershell/…。
Inside PowerShell, your location is C:\tmp. However, your working directory is still going to be C:\Users\JaneUser -
不要使用 [环境]::CurrentDirectory。它不会在 PowerShell 中跟踪您当前的目录。
标签: c# powershell filepath cmdlets