App Config and escape sequences  
Author Message
TomPearson





PostPosted: Visual C# General, App Config and escape sequences Top

Is it possible to read data from an app config file using the configuration manager without having characters being escaped automatically.

I need to read a sequence from the app config file and have it available exactly as it is written.
I.e. "\n\n\n\nABCD" should be read as "\n\n\n\nABCD", not "\\n\\n\\n\\nABCD".

Can this be done


Visual C#5  
 
 
TaylorMichaelL





PostPosted: Visual C# General, App Config and escape sequences Top

You are confusing what you see in the de**** with what is actually stored. If you have the string "\n\n\n\nABCD" (length 12) in a file then when you read it in it'll still be 12 characters long. However the de**** will show you the string with the extra slashes to clarify that it is an actual slash and not an escape sequence.

Michael Taylor - 10/26/06


 
 
TomPearson





PostPosted: Visual C# General, App Config and escape sequences Top

The problem comes when I try and do a comparison.
If agree that when I write irt out I get \n\n\n\nABCD, but when I try and compare it to a string containing \n\n\n\nABCD it fails since one conta9ins \\n\\n\\n\\nABCD and the other \n\n\n\nABCD.

 
 
TomPearson





PostPosted: Visual C# General, App Config and escape sequences Top

To expand, I am actually trying to read an escape sequence in.

 
 
TaylorMichaelL





PostPosted: Visual C# General, App Config and escape sequences Top

Could you post the code you are using to read the string in and do the comparison. Here is how I would probably test the logic, if it helps.

static void Main ( string[] args )
{
bool bFound = false;
using (StreamReader rdr = new StreamReader( ))
{
string strLine;
do
{
strLine = rdr.ReadLine();
if (strLine == ) //or if (strLine == \\n\\n\\n\\nABCD)
bFound =
true;
}
while (strLine != null);
};

if (bFound)
Console.WriteLine("Found it");
else
Console.WriteLine("Not found");
}

Michael Taylor - 10/26/06


 
 
TomPearson





PostPosted: Visual C# General, App Config and escape sequences Top

I am using the configuration manager to read the line from the app settings section.
I was basically hoping that I could avoid writing something to read a specific item since this is not an issue with all my other settings.

My code is similar to this, but the string test would come from an external source (a serial port or tcpip port)

I have tried all the comparison methods I can think of, but clearly the issue is that the ConfigurationManager sees \n as a backslash and a "n", not the escape sequence "\n" hence I get \\n.

Basically I am looking to compare to a 4 new lines followed by ABCD. However this sequence is configurable so I could be looking for anything.

I.e.
string test = "\n\n\n\nABCD";

if( doSomthing(test) == true )
{
doSomethingElse();
}

public bool doSomething(string isThisTheSame)
{
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string test = ConfigurationManager.AppSettings["Test"];

bool result = false;

if (test == isThisTheSame)
{
result = true;
}
return result;
}

 
 
James Curran





PostPosted: Visual C# General, App Config and escape sequences Top

I am actually trying to read an escape sequence in.

So, what you really want is for the .Net CLR to do the same translations at runtime that the C# compiler does as compile time.

There is no facility built-in to do that. You would have to do that manually:

string newString = oldString.Replace('\\n", "\n");



 
 
TaylorMichaelL





PostPosted: Visual C# General, App Config and escape sequences Top

So you want to convert to the escape sequences. AFAIK there is no method in the framework to parse out escape sequences. As James mentioned you'll have to do this by hand (write a function). Fortunately there are only a couple of them so something like this works:




...

Now this is inefficient since each time you'll create a temporary string. A better option is to move the string into a StringBuilder object and then use Replace to avoid creating all the copies of the strings. Also note that escape sequences like \x45 are valid as well so you might want to handle them.

BTW if you want a full function to handle this then you can steal the code from the C# parser. The source code is available from Rotor and is in csharp\sccomp\lexer.cpp. It is C++ but most of it should port pretty easily to C#.

Michael Taylor - 10/26/06


 
 
TomPearson





PostPosted: Visual C# General, App Config and escape sequences Top

Thanks for the responses.