SUN SUN - 1 year ago 80
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.

Answer Source

You can use Split() method twice like

string str = "";
string folder = (str.Split('/')[1]).Split('/')[0];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download