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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a string Value 1

Status
Not open for further replies.

chmilz

Programmer
Jan 10, 2001
94
0
0
CA
Hello all,

Another question (I am full of them today!!).

What I want to do is parse a string that holds a file name down to the file name itself....

For example, if I took the value of a "File Upload" textbox, say something like: "C:\Documents\filename.txt",
how could I parse the string into JUST the file name.

I was thinking of taking out anything BEFORE the last slash in the file name (that way a file could be in C:\Some folder\sub folder\sub folder\filename.ext" and I would still only get the file name.

I think it would probably work the easiest parsing from the end of the string... hitting the first slash and then wiping out from there.

Problem is again, I don't have the syntax base yet! If someone could help me out with a method that can do this I would really appreciate it!

thank you all for you help!
 
You no need any parsing.
Use System.IO.Path class to do everything you want:
Code:
string path = "C:\\temp\\myfile.txt";
string fileNme = Path.GetFileName(path);
string fileNameWithoutExt = Path.GetFileNameWithoutExtension(path);
string extension = Path.GetExtension(path);
string dirName=Path.GetDirectoryName(path);
string root = Path.GetPathRoot(path);
char sep = Path.PathSeparator(path);
obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top