c# - Powershell Cmdlet : Determine actual filepath of file in local directory

标签 c# powershell filepath powershell-cmdlet

我有一个自定义 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/

相关文章:

c# - 无法在单元测试中加载文件或程序集 'Newtonsoft.Json,版本 = 6.0.0.0

windows - 以加密形式存储文本并在 powershell 脚本中使用而不向其他用户泄露?

android - 是否可以将 Android 应用程序 Assets 文件的完整路径作为字符串获取?

c# - 更新面板和 Javascript

c# - 在 Swagger UI 中指示复杂输入参数对象的必需属性

windows - 检查用户是否有读/写权限

c++ - Powershell ToBase64String 和 Linux base64 之间的不同输出

ios - 重建应用程序时文档目录路径更改

windows-mobile - UWP : File. 从 Assets 文件夹中的文件读取所有字节

c# - 寻找 WCF basicHttpBinding https 教程或示例