Reputation: 2333
I have this below code which will fetch loggedin UserID
System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
//networkID=User.
string strName = p.Identity.Name;
int start;
start = strName.IndexOf("\") + 1 ;
strName = strName.Substring(start, strName.Length - start);
But start = strName.IndexOf("\") + 1 ; throwing out error saying Newline in constant. The logged in username I get is domainName\username and I want to use only username. Please help
Upvotes: 0
Views: 3857
Reputation: 33408
\
is an escape character. You either need to escape it with another \
or use verbatim string literals:
start = strName.IndexOf("\\") + 1 ;
Or:
start = strName.IndexOf(@"\") + 1 ;
Upvotes: 0
Reputation: 25694
Your backslash is escaping your last quote. use a double backslash to escape your backslash.
start = strName.IndexOf("\\") + 1 ;
Upvotes: 0
Reputation: 181104
You have to escape your backslashes by doubling them:
start = strName.IndexOf("\\") + 1 ;
Upvotes: 1
Reputation: 72930
\
is an escape character - you must use either "\\"
or @"\"
to get this to work.
Upvotes: 5