Question: I am trying to send "{" and "}" using `SendKeys.Send()`

I already tried to use `string.Replace{"{", "{{}"}`

```cs
txt = txt.Replace("{", "{{}");
txt = txt.Replace("}", "{}}");
SendKeys.Send(txt);

```

I expected it to send "{" and "}" but the Program throws an error 

> 
> System.FormatException
> 
> 
>

Answer: 
It is quite clearly documented on the [official docs](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys.send?view=netframework-4.7.2)

Here the relevant bit:

> 
> The plus sign (+), caret (^), percent sign (%), tilde (~), and
>  parentheses () have special meanings to SendKeys. To specify one of
>  these characters, enclose it within braces ({}). For example, to
>  specify the plus sign, use "{+}". **To specify brace characters, use
>  "{{}" and "{}}"**. Brackets ([ ]) have no special meaning to SendKeys,
>  but you must enclose them in braces.
> 
> 
> 

The problem with your solution is that you make 2 separate replacements.

Take the input string "{".

```
txt = txt.Replace("{", "{{}"); // input becomes "{{}"
txt = txt.Replace("}", "{}}"); // input becomes "{{{}}"
SendKeys.Send(txt);            // error!

```

I'm pretty sure there is some solution more elegant than this, but you could try something like:

```
var sb = new StringBuilder(txt.Length);
for(var i = 0; i < txt.Length; i++)
{
    var c = txt[i];
    switch (c)
    {
      case '+':
      case '^':
      case '%':
      case '~':
      case '(':
      case ')':
      case '[':
      case ']':
      case '{':
      case '}':
        sb.Append('{');
        sb.Append(c);
        sb.Append('}');
        break;
      default:
        sb.Append(c);
        break;
    }
}
SendKeys.Send(sb.ToString());

```

**Edit** extended the escaping with a switch and a for to account for the other escapable chars.
I would do it using a different variable to hold the entire string:

```
var txt = "asdf{}Asdf";
            var newString = new StringBuilder();
            for (int i = 0; i < txt.Length; i++)
            {
                if (txt[i] == '{')
                    newString.Append("{{}");
                else if(txt[i] == '}')
                    newString.Append("{}}");
                else
                    newString.Append(txt[i]);
            } 
            SendKeys.Send(newString.ToString());

```