Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Parsing part of a string, so that i only have the final segment

I am attempting in C# to remove part of a name from a string.
Using the method Bellow i am able to get all of the data from a predefined directory and place it into a Checked List Box for the user to select.  My problem is that each line looks like this

C:\Users\von Houk\Documents\Test

i am trying to get it to appear in the box as just

Test

remove all of the directory information from the string.

string[] sPatientNames;
sPatientNames = System.IO.Directory.GetDirectories(dPatientData.Path);
clbPatientSelect.Items.AddRange(sPatientNames);

Open in new window

0
cotj73
Asked:
cotj73
  • 4
  • 3
2 Solutions
 
Fernando SotoCommented:
Hi cotj73;

This will give you what you want.

clbPatientSelect.Items.AddRange(System.IO.Path.GetFileName(sPatientNames););  


Fernando
0
 
Fernando SotoCommented:
Sorry cotj73;

This is what you are looking for.

Fernando
string[] sPatientNames;
sPatientNames = System.IO.Directory.GetDirectories(dPatientData.Path);
 
for (int i = 0; i < sPatientNames.Length; i++)
{
    sPatientNames[i] = System.IO.Path.GetFileName(sPatientNames[i]);
}
 
clbPatientSelect.Items.AddRange(sPatientNames);

Open in new window

0
 
cotj73Author Commented:
two questions
clbPatientSelect.Items.AddRange(System.IO.Path.GetFileName(sPatientNames););  

this line has two ; at the end separated by a )
is that supposed to be there it comes up as an error

and also it tells me that it can not be converted from type 'string[]' to 'string'

and the one last comment that is my fault for not saying is that i am pulling folder names, not files
in my program i was using
clbPatientSelect.Items.AddRange(System.IO.Path.GetDirectoryName(sPatientNames))'
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cotj73Author Commented:
sorry commented to late on that last comment, that is excatly what i need, except that it pulles a file name, from the directory selected i need the folder names, thank you for the help!
0
 
cotj73Author Commented:
I figured it out



string[] sPatientNames;
sPatientNames = System.IO.Directory.GetDirectories(dPatientData.Path);
 
for (int i = 0; i < sPatientNames.Length; i++)
 {
   sPatientNames[i] = System.IO.Path.GetFileNameWithoutExtension(sPatientNames[i]);
 }
 
 
clbPatientSelect.Items.AddRange(sPatientNames);
 
Thank you so much for the Help

Open in new window

0
 
cotj73Author Commented:
Great Help, thank you so much sage for helping me on this problem
0
 
Fernando SotoCommented:
Very good, glad you got it working.  ;=)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now