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!

File name from PostedFile.FileName

Status
Not open for further replies.

R7Dave

Programmer
Oct 31, 2007
181
US
Hello

I am tring to create a page that will allow the user to upload a file from their harddrive, have the file name (without the directory) appear in a textbox.

I have the directory and the file name - how can I get just the file name?

Example: C:\Program Files\March\Status\WeeklyRpt.doc

How can I get just "WeeklyRpt.doc"?

I'm using...
Code:
<INPUT id="myFile" type="file" name="myFile" runat="server">


Code:
Dim sFile As String = myFile.PostedFile.FileName

Is there something like "FileName" that will not show the directory?

If I have to parse the string, how can I get the position of the last back slash in the directory plus file name?

Thanks in advance
Dave
 
var fileName = new FileInfo(myFile.PostedFile.FileName).Name
or
var fileName = new FileInfo(myFile.PostedFile.FileName).FileName

One of those should do it.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
What version of the framwork are you using? In 2.0 there is a server side file upload control that has a .FileName property that gives just the filename and no path.
 
Hello

This did the trick...

Code:
txtdir.Text = New FileInfo(myFile.PostedFile.FileName).Name

I tried this one (below) but it gave me the directory too..

Code:
myFile.PostedFile.FileName

Thanks again
Dave
 
You are correct, this will NOT work:
Code:
myFile.PostedFile.FileName
However, this will:
Code:
myFile.FileName
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top