SUN SUN - 8 months ago 39
C# Question

Get first part from url(first root directory value only) in c#

I have this url (

Now I just want to get only folder1 from this url. How can I do that? I'm getting Last root url from below code but I want only first root url.

string s = Page.Request.Url.AbsolutePath;
s = s.Substring(s.LastIndexOf("/")+1);

Pls help me to get only first directory value.


You can use Split() method twice like

string str = "";
string folder = (str.Split('/')[1]).Split('/')[0];