{simple} Extracting Substrings from textbox

G

Guest

I've been looking at .substring and .trim methods and I still have a question about extracting substrings from a textbox. Basically the textbox contains the full path to a file ... for example C:\testfile.txt or ... C:\download\temp\dlls\testfile.dll
I want to be able to extract the file name from this textbox (and put it in a string) when I click a button. Can someone illustrate how to write this line of code?
 
N

Nicholas Paldino [.NET/C# MVP]

War Eagle,

It's simple. Use the static GetFileName method on the Path class in the
System.IO namespace, like this:

// Get the filename.
string pstrFileName = System.IO.Path.GetFileName(textbox.Text);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

War Eagle said:
I've been looking at .substring and .trim methods and I still have a
question about extracting substrings from a textbox. Basically the textbox
contains the full path to a file ... for example C:\testfile.txt or ...
C:\download\temp\dlls\testfile.dll
I want to be able to extract the file name from this textbox (and put it
in a string) when I click a button. Can someone illustrate how to write
this line of code?
 
G

Guest

Exactly what I needed. Thank you!



Nicholas Paldino said:
War Eagle,

It's simple. Use the static GetFileName method on the Path class in the
System.IO namespace, like this:

// Get the filename.
string pstrFileName = System.IO.Path.GetFileName(textbox.Text);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

War Eagle said:
I've been looking at .substring and .trim methods and I still have a
question about extracting substrings from a textbox. Basically the textbox
contains the full path to a file ... for example C:\testfile.txt or ...
C:\download\temp\dlls\testfile.dll
I want to be able to extract the file name from this textbox (and put it
in a string) when I click a button. Can someone illustrate how to write
this line of code?
 
S

Shakir Hussain

This will work too.

FileInfo fInfo = new FileInfo(txtBox.text);
string fileName = fInfo.Name;

Shak.


War Eagle said:
I've been looking at .substring and .trim methods and I still have a
question about extracting substrings from a textbox. Basically the textbox
contains the full path to a file ... for example C:\testfile.txt or ...
C:\download\temp\dlls\testfile.dll
I want to be able to extract the file name from this textbox (and put it
in a string) when I click a button. Can someone illustrate how to write
this line of code?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top