Search code examples
c#escapingdouble-quotes

Syntax error with Escaping double quotes


tw.WriteLine(@" ""Air Temperature sensor "" 20," + measure + ", 24, """ + this.time.Year + "-" + this.time.Month + "-" + this.time.Day + " " + this.time.Hour + ":" + this.time.Minute + ":" + this.time.Second + "\"");

When I have three double quotes is call me I have Syntax error, ',' expected I try with four double quotes, with slash but again I have this error. I know for escaping double quotes is need two double quotes. But for my case is not working.


Solution

  • For what you are trying to achieve, the best approach is using string.Format, as follows:

    string.Format(@"""Air Temperature sensor "" 20, {0}, 24 "" {1:yyyy-MM-dd hh:mm:ss}", measure, this.time);
    

    You don't need to build the date taking the single properties when .NET can format it for you.