Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need solution for cuting path from the filename's in array?

Status
Not open for further replies.

miromiric

Programmer
Jun 6, 2005
1
0
0
AT
Hello everybody!
So as i wrote in subject, i need to cut the path from the filename's in array.
I got two listboxes and i read and compare data from the first and second and write it to third.In the listBox1 are filename's only and in listBox2 are filename's with path's.
The problem is how to cut the path from listBox2 array?
I got something like this C:\Documents and Settings\My Documents\My Pictures\mypicture.jpg and i need only mypicture.jpg.
The idea is to cut the string on the last backslash but i don't know which method is the best.

The code is here:

private void button1_Click(object sender, System.EventArgs e)
{
for (int j = 0; j < listBox1.SelectedItems.Count; j++)
for (int z = 0; z < listBox2.SelectedItems.Count; z++)

if ((listBox2.SelectedItems[j].ToString()) == listBox1.SelectedItems[z].ToString())
{

listBox3.Items.Add(listBox1.SelectedItems[z]);
}
}

Thank's for help!
Regards!
Miroslav
 
The Path class in System.IO has a static method for extracting just the filename from a path string.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
.. or maybe this will help you

// Splits the filePath into an array of substrings at the positions defined by the '\' character
string[] arrFilename = Regex.Split(yourPath,@"\\",System.Text.RegularExpressions.RegexOptions.None);
// Reverse the array so that the fileName become the first
Array.Reverse(arrFilename);
yourFileName = arrFilename[0];
 
I learn something new everytime I come on here...

I always do it this way, seems a little less eloquent than regular expressions mind you

// using LastIndexOf
sFileName = arrFilename[0].Substring((arrFilename[0].LastIndexOf( "\\" ) ) + 1 );

I haven't used the Path class, but that seems like a great idea as well.

------------------------------------------
Steen Bray
Kermode Computing Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top