Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hello guys

I want to cut out a part of a path but don't know how.
To get the path, I use this code:
C#
String path = System.IO.Path.GetDirectoryName(fullyQualifiedName);

(path = "Y:\\Test\\Project\\bin\\Debug")

Now I need the first part without "\\bin\\Debug".
How can I cut this part out of the current path?


I'm thankful for all answers.

Greetings
Epanjohura
Posted
Updated 21-Jul-13 20:35pm
v2
Comments
Sergey Alexandrovich Kryukov 19-Jul-13 9:21am    
Why "MFC"? What prevents you from just reading the documentation?
—SA
[no name] 19-Jul-13 9:22am    
Pick a programming language first. The answers you get are probably going to be vastly different. But for either one you chose, using a combination of SubString and IndexOf will probably do the trick.
Sergey Alexandrovich Kryukov 19-Jul-13 11:25am    
It's C#, "MFC" could be a slip of a hand. :-)
—SA
ridoy 19-Jul-13 11:10am    
do you want it in C# or C++?
epanjohura 22-Jul-13 2:35am    
c#, my fault.. but thanks for every help, I solved the problem

You don't mention whether you want it in C++ or C#. But your coding pattern seems me it is in C#. Another question,is your above path static?
If so then you can do it within only 3 lines.
C#
String path= @"Y:\\Test\\Project\\bin\\Debug";
String[] extract = Regex.Split(path,"bin");  //split it in bin
String main = extract[0].TrimEnd('\\'); //extract[0] is Y:\\Test\\Project\\ ,so exclude \\ here
Console.WriteLine("Main Path: "+main);//get main path
 
Share this answer
 
v3
Your tags are a bit puzzling since you tagged C# and MFC, which don't play well together. The answers you have so far are related to C#. If you are using MFC, that means C++, and you can use _splitpath()/_wsplitpath()[^] and then either std::string[^] or CString[^] to extract or replace parts of the path.

CString works with ANSI or Unicode, std::string is ANSI only. Both have their own vocal self righteous religious lobbies.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Jul-13 11:24am    
It's a good point, even though the question is apparently on .NET, 5ed.
(Ha, someone decided to down-vote both answers. Funny :-)
—SA
Maciej Los 19-Jul-13 14:35pm    
Good point!
+5!
epanjohura 22-Jul-13 2:36am    
thank you so much for your help
You cannot modify any string; strings are immutable, so all string methods changing the string actually create a new string object. You can use this one: http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx[^].

—SA
 
Share this answer
 
Comments
Maciej Los 19-Jul-13 9:36am    
String.Replace function... This is it!
+5!
Sergey Alexandrovich Kryukov 19-Jul-13 9:43am    
Thank you, Maciej.
—SA
Hi . i think the simplest way is :

string s=path.Substring(path.IndexOf('Y'),15);

in this line of code you get a substring by position of 'Y' character.
and the count of forwarding to get substring, is 15 character.
i hope this help you.
Good luck
 
Share this answer
 
Comments
epanjohura 23-Jul-13 8:07am    
I already have a solution but thanks for your help
aliwpf 24-Jul-13 6:49am    
you're wecolme.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900