Escape characters when generating Powershell scripts using C#

后端 未结 1 1437
借酒劲吻你
借酒劲吻你 2021-01-20 19:40

I use VS2010, C#, .NET 3.5 for generate Powershell scripts (ps1 files).

Then, it is required escape characters for Powershell.

Any suggestions about it for d

相关标签:
1条回答
  • 2021-01-20 19:46

    The other option would be to use a regex:

    private static Regex CharactersToEscape = new Regex(@"['""]"); // Extend the character set as requird
    
    
    public string EscapeForPowerShell(string input) {
      // $& is the characters that were matched
      return CharactersToEscape.Replace(input, "`$&");
    }
    

    Note: you don't need to escape backslashes: PowerShell does not use them as escape characters. This makes writing regexes somewhat easier.

    0 讨论(0)
提交回复
热议问题