.net - How build a string in C# with escape characters -
i'm trying build directory path injecting userid
, portalid
needed.
string userid = httpcontext.current.request.params["userid"]; string portalid = httpcontext.current.request.params["portalid"]; // acutal path ---> string foldername = @"c:\dotnetnuke 8.0\portals\0\users\017\17\17"; string foldername = "c:\\dotnetnuke 8.0\\portals\\" + portalid + "\\users\\0" + userid + "\\" + userid + "\\" + userid; httpcontext.current.response.write(foldername); // what's returned ---> c:\dotnetnuke 8.0\portals\\users\0\\
what have correct. looks portalid
, userid
null
or empty string ""
. there easier ways escape, though - if use @"some \ string"
, thing need escape "
""
.
examples:
string userid = "17"; string portalid = "0"; string foldername = "c:\\dotnetnuke 8.0\\portals\\" + portalid + "\\users\\0" + userid + "\\" + userid + "\\" + userid;
gives
c:\dotnetnuke 8.0\portals\0\users\017\17\17
as does:
string foldername = @"c:\dotnetnuke 8.0\portals\" + portalid + @"\users\0" + userid + @"\" + userid + @"\" + userid;
as does:
string foldername = $@"c:\dotnetnuke 8.0\portals\{portalid}\users\0{userid}\{userid}\{userid}";
Comments
Post a Comment