If i have a such as path \-edd\fold\fold2\ or parent\fold\fold2\ how do i get the value of -edd or parent from the path? I tried .Root but that just returns C:\, its a relative path

If i have a such as path \-edd\fold\fold2\ or parent\fold\fold2\ how do i get the value of -edd or parent from the path?  I tried .Root but that just returns C:\, its a relative path
cbruneAsked:
Who is Participating?
 
Todd GerbertConnect With a Mentor IT ConsultantCommented:
Something along the lines of:
string topLevel = String.Empty;
string path = @"\-edd\fold\fold2\";
string[] pieces = path.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
if (pieces.Length > 0)
	topLevel = pieces[0];

Open in new window

0
 
Todd GerbertIT ConsultantCommented:
You can split the string on the \ and get the first element of the resulting array.
@"\-edd\fold\fold2\".Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries)[0];

Open in new window


Of course, in reality, you check to make sure the Split method returned an array with at least one elements before you try to refer to it.
0
 
cbruneAuthor Commented:
now how would i strip out the \-edd out of the path and have \fold\fold2?
0
 
Todd GerbertIT ConsultantCommented:
Have a look at arrays in C# (http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx) and the String.Split() method (http://msdn.microsoft.com/en-us/library/ms131448.aspx).

Try using my example above (http:#a35325655) and put a break-point on line 3, and then inspect the contents of the pieces string array.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.