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

How to get File Name? 2

Status
Not open for further replies.

whloo

Programmer
Apr 14, 2003
168
SG
Hi,

I am trying to get the file name of my current accessed file.
how can i get it?
example, i am now in test.aspx. i want to get the 'test' file name. what is the corrent syntax i have to use?
thanks!

regards,
weihann.
 
you will probably have a FileStream object somewhere. get the full name using FileStream.name, then copy from the last occurence of "/", to the last occurence of ".".

FileStream myFile = new FileStream("C:/file.txt", FileMode.Open);

string fullName = myFile.name;
int nameFrom = fullName.LastIndexOf("/");
int nameTo = fullName.LastIndexOf(".");

string filename = fullName.Substring(nameFrom, nameTo-nameFrom);

- i think this should work. my apologies if you end up being a character out.

____________________________________________________
If you like a post, show you care by giving it a star.
 
seanbo -
Please see thread732-637680

whloo -
How are you accessing this file? Do you have it opened as a stream? Or are you saying "How do I find out the name of the code module that is currently executing"?

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
thanks for all of ur reply.
lets say i have a web sites name as so, when i am i i want to know what is the file name that i am currently access to. For this example, i will expect to get a file name with index.aspx but i want only the file name excluding the extension.
thanks!
 
You can look at the Request object, which is part of the Page object that your code is running in (the this object).

Off the Request object, you have the FilePath property, which you can then pass to a Path object to get just the filename part:
Code:
String MyPath = this.Request.FilePath;
String Myfilename = Path.GetFileNameWithoutExtension(MyPath);
You'll need to reference the System.IO namespace, and maybe set the proper permissions to use the Path object (give it a try first).

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
thanks for ur reply chiph!
it certainly all the stuff i want.
too bad i don't really like it when i need to include the namespace for some reason.
thanks anyway :)
a star to u :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top