Header Ads

Different ways of getting path using ASP.NET and C#

ASP.NET and C#

1.Server.MapPath - Server.MapPath returns the Physical path of the location from the Current directory for an http request. You can use ~ sign to determine whether it is relative to the current application path.
string path = Server.MapPath("~/myappfolder");

2.Environment.CurrentDirectory - The path returned is the current directory path of the Project. Generally if Environment path is set before executing the assembly, and thus it generally gives the Base directory path of the executing assembly, but working directory can change often even during application lifetime.
var directory = Environment.CurrentDirectory;

3.HttpRuntime.AppDomainAppPath - The path returned from the above code is the application directory path for the hosted application. For hosted application in IIS or from ASP.NET.
string webCurrentDirectory = HttpRunTime.AppDomainAppPath;

4.Assembly.GetExecutingAssembly().Location -The path returned by the above code is the UNC path of the loaded file. This path generally conforms to the actual UNC path of the assembly file loaded to execute the code.
Assembly asmly = Assembly.GetAssembly(myobj.GetType()); // Assembly.GetExecutingAssembly();
Console.WriteLine(asmly.Location);
//To get the Directory path
string theDirectory = System.IO.Path.GetDirectoryName(asmly.Location);

5.Assembly.GetExecutingAssembly().CodeBase - The path returned by the above code is the actual path which was specified originally. If the assembly is downloaded from a server, it could virtually hold a http:// location, and cannot ensure it is to be a valid physical path always. If the assembly is installed into GAC, the path does not ensure its set.
Assembly asmly = Assembly.GetAssembly(myobj.GetType()); // Assembly.GetExecutingAssembly();
Console.WriteLine(asmly.CodeBase);

No comments:



Powered by Blogger.