C#: How to Get details of the directory where the currently running program is located

1.asp.net webform

  1.1 Use “Request.PhysicalApplicationPath to get the physical path of the virtual directory where the site is located, and finally include “\”;

2. In c# winform
  2.1 “Application.StartupPath”: Get the path of the directory where the current application is located, excluding “\” at the end;
  2.2 “Application.ExecutablePath”: Get the path of the current application file, including the name of the file;
  2.3 “AppDomain.CurrentDomain.BaseDirectory”: Get the path of the directory where the current application is located, including “\” at the end;
  2.4 “System.Threading.Thread.GetDomain().BaseDirectory”: Get the path of the directory where the current application is located, including “\” at the end;
  2.5 “Environment.CurrentDirectory”: Get the path of the current application, without “\” at the end;
  2.6 “System.IO.Directory.GetCurrentDirectory”: Get the path of the current application, excluding “\” at the end;
3. c# windows service service
  3.1 “AppDomain.CurrentDomain.BaseDirectory” or “System.Threading.Thread.GetDomain().BaseDirectory”;
  3.2 “Environment.CurrentDirectory” and “System.IO.Directory.GetCurrentDirectory” will get the path to the “system32” directory;
       Note : If you want to use “Application.StartupPath” or “Application.ExecutablePath”
       You need to manually add a reference to “System.Windows.Forms.dll” and declare the reference with “using System.Windows.Forms” at the beginning of the program;
4. Obtain the system installation directory in the uninstaller:
  4.1System.Reflection.Assembly curPath = System.Reflection.Assembly.GetExecutingAssembly();
              string path=curPath.Location;//Get the path of the installer class SetupLibrary file, get the directory where the file path is located to get the directory of the installer;
 4.2System.Diagnostics.StackFrame f = new System.Diagnostics.StackFrame(1);
              MethodBase mb = f.GetMethod();
              System.Web.HttpContext.Current.Response.Write(mb.DeclaringType.ToString()); Get the information of the calling class, you can know the situation of the subclass from the parent class

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *