File name designation inside app.config file using c #

Is it possible to pin file names inside the app.config file and use them in your code?

For example, this code works fine:

string folder = @"C:\CDR_FTP1\ttfiles_exported\"; 

If I want to get the above value from app.config file

  • Can this be done?
  • What will be the syntax?

For example, inside app.config, I could try something like this:

 <add key="InputDir" value=@ "D:\CDR_FTP1\ttfiles_exported\" /> 

I cannot add "@" because it will give me the following error:

 "The character '@', hexadecimla value 0x40 is illegal at the beginning of an XML name." 

If I try to use it in my code as shown below, that’s fine, but where and how will I need to stick with “@” so that the application can read it correctly?

 string folder = ConfigurationSettings.AppSettings["InputDir"]; 
+4
source share
2 answers

Just use:

 <add key="InputDir" value="D:\CDR_FTP1\ttfiles_exported\" /> 

The XML attribute does not need to avoid \ .

+9
source

To expand Cyril's answer:

Special characters in XML attributes are different from C # special characters.

In C #, the character '\' is a string escape character, which is used to denote escape sequences, such as '\ n' for the end of a string, as you probably know; and therefore, you must avoid either "\\" or using @ ".

In XML, '\' has no special meaning inside attribute values ​​and can be used as is. This is not true for "&" and '' 'characters. If you want to include the value:

 M & M "The chocolates" 

in app.config, they should be escaped like this:

 <add key="SomeString" value="M &amp; M &quot;The chocolates&quot;" /> 
+9
source

All Articles