473,511 Members | 16,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1277
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
3633
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
5452
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
5482
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
3598
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
3694
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
2100
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
26524
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
2140
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
2306
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
2783
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
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
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
7353
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
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
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,...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.