Hi, I am trying to get the current page file name that i access to. Lets say i am at test.aspx page, i want it to return me "test" file name. I tried to use this.Page.toString(), it returned me "ASP.test_aspx" which is like half correct.... but i would like
to get only "test". 2nd question, this.Page.toString() will always return me in a format of ASP.[filename]_[fileextention]? If that's the case, maybe i can use splilt function to get the file name i want if there aren't any better solution than that. Thanks!
regards, weihann.
To save some parsing you could use Request.RawUrl, you will still need to parse out the extension. You might want to consider posting an argument to the page with the data you want - then you can easily query the page for that argument. HTH JD
This posting is provided "AS IS" with no warranties, and confers no rights.
We Are Hiring
also you can use string pageName = this.Page.ToString().Substring(4,this.Page.ToString().Substring(4).Length - 5) + ".aspx";
C# ( pronounced as C-sharp ) is a new Java like language from Microsoft. Microsoft says that C# is a language with the power of C++ and simplicity of Visual Basic. C# supposed to be the best language for Microsoft's .NET programming
To clarify a bit on this, the "ASP.test_aspx" that ToString() returns is a result of the default implementation of Object.ToString(), which is to return Object.GetType().FullName. Basically, "ASP._" is ASP.NET's internal format for generating strongly-typed
objects based on the contents of a file. You should not depend on this behaviour, as it is an internal detail of ASP.NET's implementation, and is by no means guaranteed to remain the same in future versions. My advice would be to use Request.PhysicalPath
to get the physical location of the requested file, then use the System.IO.Path.GetFileNameWithoutExtension() method to extract only the file's name. Hope this helps. -- Ryan Milligan
It depends on what is the actual purpose - as many things can happen from the user request to the acual servicing of the request, you might want to use Request.PhysicalPath, you might also want to use the CurrentExecutionFilePath (this will take Server.Transfer
etc into account).
You might also be interested in the actual request (after rewrite), and use Request.Url.AbsolutePath .
Finally (?) if you want the unmodified original use new Uri(Request.RawUrl).Absolute.Path.
In cases where a physical path is involved, use System.IO.Path.GetFileName() and System.IO.Path.GetExtension() methods to extract parts of it, and use System.Web.VirtualPathUtility.GetFileName() and GetExtension() methjods when it's a part of an URL or is
a virtual path.
Why can't things just be simple? ;-)
Svante
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com [Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
Marked as answer by TATWORTH on Apr 06, 2010 11:58 AM
whloo
Member
185 Points
37 Posts
How to get current page file name?
Aug 25, 2003 03:50 PM|LINK
jdixon
Contributor
2210 Points
438 Posts
Microsoft
Re: How to get current page file name?
Aug 25, 2003 05:44 PM|LINK
We Are Hiring
FRaas
Member
620 Points
136 Posts
Re: How to get current page file name?
Aug 26, 2003 12:05 AM|LINK
Ryan Milliga...
Participant
1057 Points
210 Posts
Re: How to get current page file name?
Aug 28, 2003 06:33 PM|LINK
gllort
Member
2 Points
1 Post
Re: How to get current page file name?
Dec 13, 2007 07:52 AM|LINK
Try this:
file properties system.io.file c#.net ASP.net 2.0 .net 2.0 c# filename c#
Svante
All-Star
18347 Points
2300 Posts
Re: How to get current page file name?
Dec 17, 2007 07:00 AM|LINK
It depends on what is the actual purpose - as many things can happen from the user request to the acual servicing of the request, you might want to use Request.PhysicalPath, you might also want to use the CurrentExecutionFilePath (this will take Server.Transfer etc into account).
You might also be interested in the actual request (after rewrite), and use Request.Url.AbsolutePath .
Finally (?) if you want the unmodified original use new Uri(Request.RawUrl).Absolute.Path.
In cases where a physical path is involved, use System.IO.Path.GetFileName() and System.IO.Path.GetExtension() methods to extract parts of it, and use System.Web.VirtualPathUtility.GetFileName() and GetExtension() methjods when it's a part of an URL or is a virtual path.
Why can't things just be simple? ;-)
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
[Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
Obiora
Member
2 Points
1 Post
Re: How to get current page file name?
Mar 22, 2010 01:22 PM|LINK
Thanks man. Yours worked and also worked fine too
kanasz.rober...
Contributor
3242 Points
523 Posts
Re: How to get current page file name?
Mar 22, 2010 03:27 PM|LINK
Hi,
try this:
string filename = Path.GetFileName(Request.Path);
regards
Robert
- SQL Server 2008, Database Development
- SQL Server 2008, Implementation and Maintenance
MCPD - ASP.NET Developer 3.5
- Web Developer 4
MCITP - Database Administrator 2008
- Database Developer 2008
martinEspi
Member
2 Points
1 Post
Re: How to get current page file name?
Jul 08, 2010 03:53 AM|LINK
I've also tried de Page.ToString() but If MS change this it won't work anymore.
I've finally use this to get only the filename + extension of the file
string[] file = Request.CurrentExecutionFilePath.Split('/');
string fileName = file[file.Length-1];
Pretty basic, I hope this help.
Martin.
AlexanderB
Participant
801 Points
163 Posts
Re: How to get current page file name?
Aug 27, 2010 07:02 PM|LINK
Thanks a lot, it helped me!
Just because it is interesting