How to use special characters in strings?

4

What are the ~ and @ characters in the path string in ASP.NET. Example:

StreamReader srFile = new StreamReader(@"\server\pasta\arquivo.html");
    
asked by anonymous 04.02.2014 / 14:01

2 answers

3

In ASP.NET path you have some 'shortcuts' to specify the path.

Let's assume your project is on the following path C:\Projetos\TestandoOPath

And you will use the code on the page that is in http://localhost:<porta>/TestandoOPath/Pagina/Teste/index.aspx

You can use the following options:

  • Server.MapPath ("."): Returns the physical directory of the file that is executing this operation in the C:\Projetos\TestandoOPath\Pagina\Teste
  • Server.MapPath (".."): returns a previous directory to the current directory in C:\Projetos\TestandoOPath\Pagina
  • Server.MapPath ("~"): returns the root of the application in the C:\Projetos\TestandoOPath

And the use of @ is to treat text without the escape character effect .

Example: new StreamReader(@"\server\pasta\arquivo.html"); if you do not put @ you need to put another \ getting "\server\pasta\arquivo.html" to null the escape effect of it.

More information about 'shortcuts' in the path can be seen here in English OS .

    
04.02.2014 / 14:33
2

The symbol @ , before a literal string, serves to avoid escaping the special characters of the string.

@"\server\pasta\arquivo.html"

is equivalent to:

"\server\pasta\arquivo.html"

The @ symbol can also be used to use keywords as variable names:

int class; //erro de compilação, a palavra "class" esta reservada
int @class; //funciona

The ~ operator is the complement of a (or bitwise negation). Serves to deny all bits.

~ 1001 = 0110

Links: One's complement operator / Complemento de um

    
04.02.2014 / 14:03