ASP.NET File Upload Control Question

This seems really simple. But yet I must ask ... I am using ASP.Net FileUpload control. How to get just the file name of the uploaded file ? Am currently using  

string fileName = FileUpload1.PostedFile.FileName

This returns the full path plus the filename  from my client machine. (Ex C:\User\Desktop\myfile.txt)  Any advice on how to get just the file name (myfile.txt) would be most appreciated
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Here is reference to string functions you could use, if you combine searching for the last '\' with getting the substring that follows you should get the name of the file.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BTW if the path is always the exact same and is known beforehand then you could search for that pattern in the string and replace it with nothing '' and then you are left with the file name!!

if you do not know the path in advance then you will have to search for the last '\' as i suggested above and then copy what comes after the last \ until you have the file name.
You should be able to get what you're looking for with this simply enough:

string fName = FileUpload1.FileName;
Carlos VillegasFull Stack .NET DeveloperCommented:
ddayx10 is correct, you just must use the FileName property of the System.Web.UI.WebControls.FileUpload class.

Also, when you want to extract only the file name of a path you can use this function, example:
string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);

Open in new window

jazzcatoneAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.