How to use special characters in strings?

Asked

Viewed 3,054 times

5

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

StreamReader srFile = new StreamReader(@"\\server\pasta\arquivo.html");
  • The problem has no relation to ASP.NET. I edited to improve this but kept in part to leave your original intention. You could cite an example using ~. Then you will see that it has no relation to the problem or will show that your doubt with this character is another. Try to make it clearer to get better answers.

2 answers

4


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

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

And you will use the code on the page that is on 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 running this operation in the case C:\Projetos\TestandoOPath\Pagina\Teste
  • Server.Mappath("."): returns a previous directory to the current one in the case C:\Projetos\TestandoOPath\Pagina
  • Server.Mappath("~"): returns the application root in the case C:\Projetos\TestandoOPath

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

Example: new StreamReader(@"\server\pasta\arquivo.html");if you nay put the @ you need to put another \ getting "\\server\\pasta\\arquivo.html" to override its escape effect.

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

2

The symbol @, before a literal string, serves to avoid escape 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 name:

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

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

~ 1001 = 0110

Links: One’s Complement Operator / Complement to a

Browser other questions tagged

You are not signed in. Login or sign up in order to post.