473,396 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help getting a directory name from a path

Hi all,

I'm having problems identifying how to get the last directoy in a path string.

For example, in the Path 'C:\dir1\dir2\dir3'

I want to be able to get the name dir3

I've tried the following but for some reason it doesnt split the string at
all. It just returns the full string.

string[] splitPath = fullPath.Split('\\');

if(splitPath != null && splitPath.Length > 0){
dirName = splitPath[splitPath.Length - 1];
}

return dirName;

Ideally I'd also like to be able to identify the filename in a full path
string aswell, but I dont know how to do that either

Many thanks to anyone who could provide the magic incantations

Kindest Regards

Simon

Jul 21 '05 #1
5 1271
Take a look at the Path class.

"SimonH" <no**@hotmail.com> wrote in message
news:68*********************@news.microsoft.com...
Hi all,

I'm having problems identifying how to get the last directoy in a path string.
For example, in the Path 'C:\dir1\dir2\dir3'

I want to be able to get the name dir3

I've tried the following but for some reason it doesnt split the string at
all. It just returns the full string.

string[] splitPath = fullPath.Split('\\');

if(splitPath != null && splitPath.Length > 0){
dirName = splitPath[splitPath.Length - 1];
}

return dirName;

Ideally I'd also like to be able to identify the filename in a full path
string aswell, but I dont know how to do that either

Many thanks to anyone who could provide the magic incantations

Kindest Regards

Simon

Jul 21 '05 #2
I'd suggest that you should start with the Path class:
http://msdn.microsoft.com/library/en...ClassTopic.asp

As far as your Split statement goes, the following snippet works for me:

string MyPath = @"c:/temp/second/third/myfile.txt";
string[] parts = MyPath.Split('/');
foreach (string part in parts)
Console.WriteLine(part);

HTH,
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"SimonH" <no**@hotmail.com> wrote in message
news:68*********************@news.microsoft.com...
Hi all,

I'm having problems identifying how to get the last directoy in a path
string.

For example, in the Path 'C:\dir1\dir2\dir3'

I want to be able to get the name dir3

I've tried the following but for some reason it doesnt split the string at
all. It just returns the full string.

string[] splitPath = fullPath.Split('\\');

if(splitPath != null && splitPath.Length > 0){
dirName = splitPath[splitPath.Length - 1];
}

return dirName;

Ideally I'd also like to be able to identify the filename in a full path
string aswell, but I dont know how to do that either

Many thanks to anyone who could provide the magic incantations

Kindest Regards

Simon

Jul 21 '05 #3

Your code is fine. The problem lies with the contents of fullpath. How are
you populating fullpath? Run the following code to verify the same.

string[] fullPath=Directory.GetDirectories("c:\\");
string dirName="";
string[] splitPath = fullPath[0].Split('\\');
if(splitPath != null && splitPath.Length > 0)
{
dirName = splitPath[splitPath.Length - 1];
}

"SimonH" wrote:
Hi all,

I'm having problems identifying how to get the last directoy in a path string.

For example, in the Path 'C:\dir1\dir2\dir3'

I want to be able to get the name dir3

I've tried the following but for some reason it doesnt split the string at
all. It just returns the full string.

string[] splitPath = fullPath.Split('\\');

if(splitPath != null && splitPath.Length > 0){
dirName = splitPath[splitPath.Length - 1];
}

return dirName;

Ideally I'd also like to be able to identify the filename in a full path
string aswell, but I dont know how to do that either

Many thanks to anyone who could provide the magic incantations

Kindest Regards

Simon

Jul 21 '05 #4

Hi, the problem lies in how you are populating fullpath. Check the following
code which works fine.

string[] fullPath=Directory.GetDirectories("c:\\");
string dirName="";
string[] splitPath = fullPath[0].Split('\\');
if(splitPath != null && splitPath.Length > 0)
{
dirName = splitPath[splitPath.Length - 1];
}

"SimonH" wrote:
Hi all,

I'm having problems identifying how to get the last directoy in a path string.

For example, in the Path 'C:\dir1\dir2\dir3'

I want to be able to get the name dir3

I've tried the following but for some reason it doesnt split the string at
all. It just returns the full string.

string[] splitPath = fullPath.Split('\\');

if(splitPath != null && splitPath.Length > 0){
dirName = splitPath[splitPath.Length - 1];
}

return dirName;

Ideally I'd also like to be able to identify the filename in a full path
string aswell, but I dont know how to do that either

Many thanks to anyone who could provide the magic incantations

Kindest Regards

Simon

Jul 21 '05 #5
Thanks everyone

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kali K E | last post by:
Hi, I could not understand how I can do the following things in Python. Please help me. 1. First I have to find the current directory from where the script is invoked. 2. Next I have to form a...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
1
by: Alpha | last post by:
I have a Window based application that shows up still running in the task manager when I close it. It reaches the "this.close" statement and then it stops at the "}" at the section of the...
13
by: Siegfried Heintze | last post by:
I refered the engineer at my hosting service to http://support.microsoft.com/default.aspx?scid=kb;en-us;825738 where he tried to follow the directions there. He said there was no such file:...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
4
by: thoseion | last post by:
Hi, I am trying to get a program working whereby directory and file names are read into a list. I have been given the original list structure - it appears that the directory names should be added...
11
by: cybervigilante | last post by:
I can't seem to change the include path on my local winmachine no matter what I do. It comes up as includ_path .;C:\php5\pear in phpinfo() but there is no such file. I installed the WAMP package...
5
by: ibid | last post by:
hi every one just wondering if anyone could help sorry if i seem abit dumb but im a newbie to it all ive got to asign a mail program on my auction site in the config.pl files (i think this is the...
8
by: inFocus | last post by:
Hello, I am new to python and wanted to write something for myself where after inputing two words it would search entire drive and when finding both names in files name would either copy or move...
13
by: lawpoop | last post by:
Hello all - I have a two part question. First of all, I have a website under /home/user/www/. The index.php and all the other website pages are under /home/user/www/. For functions that are...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.