-2

Hi I want to append the line

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

to a text file but I am stuck at the "C#" , "WebForm1.aspx.cs" and "WebApplication1.WebForm1" part my code is as below

 public string HeaderContent1()
 {
     var sb = new StringBuilder();
     sb.Append("<%@ Page Language=");
     sb.Append("c#");// prints only c#
     sb.Append("AutoEventWireup=");
     sb.Append("true");
     sb.Append("CodeBehind=");
     sb.Append("WebForm1.aspx.cs");// prints only WebForm1.aspx.cs I want with double quotes
     sb.Append("WebApplication1.WebForm1");// prints only WebApplication1.WebForm1 I want with double quotes
     sb.Append("%>");

     sb.AppendLine();
     sb.Append("<html>");
     sb.AppendLine();// which is equal to Append(Environment.NewLine);
     sb.Append("<head>");
     sb.AppendLine();
     sb.Append("<h1>New File header</h1>");
     sb.AppendLine(); // which is equal to Append(Environment.NewLine);
     sb.Append("</head>");
     sb.AppendLine();
     sb.Append("<body>");
     sb.AppendLine(); // which is equal to Append(Environment.NewLine);
     sb.Append("<h2> New File Body</h2>");
     sb.AppendLine();

     sb.Append("</body>");
     sb.AppendLine();

     sb.Append("</html>");
     sb.AppendLine();
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Pritam Panda
  • 45
  • 1
  • 1
  • 11
  • i want to append <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> – Pritam Panda Nov 20 '14 at 05:53
  • possible duplicate of [Can I escape a double quote in a verbatim string literal?](http://stackoverflow.com/questions/1928909/can-i-escape-a-double-quote-in-a-verbatim-string-literal) – steve cook Nov 20 '14 at 05:56

2 Answers2

5

You have a couple options when trying to use a quote inside a quoted string.

You can escape the quotations:

sb.Append("\"WebForm1.aspx.cs\"");

Or use a verbatim symbol:

sb.Append(@"""WebForm1.aspx.cs""");

You could eliminate the StringBuilder altogether:

var page = "<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"WebForm1.aspx.cs\" Inherits=\"WebApplication1.WebForm1\" %><html>\r\n<head>\r\n<h1>New File header</h1>\r\n</head>\r\n<body>\r\n<h2> New File Body</h2>\r\n</body>\r\n</html>";
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
2

You can use @ before the string and add "" where you want to print " for example

sb.Append(@"""c#""");

will print "c#". Here's the complete modified code

var sb = new StringBuilder();
sb.Append(@"<%@ Page Language=");
sb.Append(@"""c#""");
sb.Append(" AutoEventWireup=");
sb.Append(@"""true""");
sb.Append(" CodeBehind=");
sb.Append(@"""WebForm1.aspx.cs""");
sb.Append(" Inherits=");
sb.Append(@"""WebApplication1.WebForm1""");
sb.Append("%>");
sb.AppendLine();
sb.Append("<html>");
sb.AppendLine();
sb.Append("<head>");
sb.AppendLine();
sb.Append("<h1>New File header</h1>");
sb.AppendLine();
sb.Append("</head>");
sb.AppendLine();
sb.Append("<body>");
sb.AppendLine();
sb.Append("<h2> New File Body</h2>");
sb.AppendLine();
sb.Append("</body>");
sb.AppendLine();
sb.Append("</html>");
sb.AppendLine();

Working demo: https://dotnetfiddle.net/eEGgUJ

ekad
  • 14,436
  • 26
  • 44
  • 46