Search code examples
c#stringquotes

C# - How to get source code of a program in string variable for parsing?


I am trying to parse the code of a MainForm.cs file. As you can imagine it contains texts like:

string name = "Name: "+var1;

I presume the code below will just break on the first quotes it finds?

string text = System.IO.File.ReadAllText(@"C:\Project\MainsForm.cs");

So what would work?


Solution

  • The ReadAllText method will read the entire file, quotation marks and all. When you parse it, the string will contain quotation marks. There is nothing in C# that prevents strings from containing quotation marks. For example, one could define a string as:

    string quotedValue = "This string contains some \"air quotes\"";
    

    And it would work.