asp. net programming to get the project root directory implementation method collection

  • 2021-07-09 07:49:29
  • OfStack

This paper summarizes the implementation method of asp. net programming to obtain the root directory of the project. Share it for your reference, as follows:

When writing programs, you often need to use the project root directory. My summary is as follows

1. Get the root directory method of the console application

Method 1. Environment. CurrentDirectory Gets or sets the full qualified path of the current working directory
Method 2, AppDomain. CurrentDomain. BaseDirectory Get the base directory, which is used by the assembly resolver to probe the assembly

2. Get the root directory method of Web application

Method 1, HttpRuntime. AppDomainAppPath. ToString (); //Gets the physical drive path of the application directory of the application hosted in the current application domain. Used in App_Data to get
Method 2, Server. MapPath ("") or Server. MapPath ("~/"); //Returns the physical file path relative to the specified virtual path on the Web server
Method 3, Request. ApplicationPath; //Get the virtual application root of the ASP. NET application on the server

3. Get the root directory method of WinForm application

① Environment. CurrentDirectory. ToString (); //Gets or sets the fully qualified path to the current working directory
② Application. StartupPath. ToString (); //Gets the path of the executable file that started the application, excluding the name of the executable file
③ Directory. GetCurrentDirectory (); //Get the current working directory of the application
④ AppDomain. CurrentDomain. BaseDirectory; //Gets the base directory, which is used by the assembly resolver to probe the assembly
⑤ AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //Gets or sets the name of the directory that contains the application

Among them: the following two methods can get the execution file name

1. Process. GetCurrentProcess (). MainModule. FileName; //Get the file name of the currently executing exe.
2. Application. ExecutablePath; //Get the path to the executable file that started the application, including the name of the executable file

I believe that many friends who use asp. net + Access as websites often have such a demand: they want to get the physical path of Access database in the database access layer class library, and then splice the database connection string for database-related operations. In the website UI layer, we can have many ways to get the physical path of a website, such as:

1. Request.PhysicalApplicationPath

2. Request. MapPath ("~/"), but not in the database access layer


using System.Reflection;
using System.IO; // Don't forget to reference these two namespaces before using them 
/// <summary>
///  Get Access Physical path of database 
/// </summary>
/// <returns></returns>
public static string GetDBPath()
{
  string str = Assembly.GetExecutingAssembly().Location;
  str = Path.GetDirectoryName(str) + @"\__AssemblyInfo__.ini";
  str = File.ReadAllText(str, System.Text.Encoding.Unicode);
  int index = str.IndexOf("file:///") + 8;
  int length = str.IndexOf("/bin");
  str = str.Substring(index, length - index);
  str = str.Replace('/', '\\');
  str += @"\App_Data\DB.mdb";
  return str;  // The last thing returned is the physical path of the database .
}

Code explanation:

1. string str = Assembly.GetExecutingAssembly().Location;

The obtained value is a temporary directory, such as: "C:\ WINDOWS\ Microsoft. v\ Framework\ v2.0. 50727\ Temporary ASP. NET Files\ ba81bed7\ a7082081\ assembly\ dl3\ 62f82680\ 8345eb5b_37a6c901\ abc. dll

2.str = Path.GetDirectoryName(str) + @"\__AssemblyInfo__.ini";

The key is this sentence. There is a file "__AssemblyInfo _. ini" under the same directory as dll file. Open it with Notepad and find the actual physical address of dll. It is easy to know this.

3.


str = File.ReadAllText(str, System.Text.Encoding.Unicode);
int index = str.IndexOf("file:///") + 8;
int length = str.IndexOf("/bin");
str = str.Substring(index, length - index);
str = str.Replace('/', '\\');
str += @"\App_Data\cms.mdb";

These codes are not explained in detail, that is, read out the ini file, find out the actual physical path of the website bin folder from inside, and then splice the database file name, and get the physical path of the database, but this method is effective on the premise that the database file is in the same directory as the website.

I hope this article is helpful to everyone asp. net programming.


Related articles: