Get the root directory method of the console application
1:environment.currentdirectory Gets or sets the fully qualified path to the current working directory
2:appdomain.currentdomain.basedirectory gets the base directory, which is used by the Assembly resolver to probe assemblies
Get the root directory method for the WinForm application
1:environment.currentdirectory.tostring ();//Gets or sets the fully qualified path of the current working directory
2:application.startuppath.tostring ();//Gets the path to the executable file that launched the application, not including the name of the executable file
3:directory.getcurrentdirectory ();//Gets the current working directory of the application
4:appdomain.currentdomain.basedirectory;//gets the base directory, which is used by the Assembly resolver to probe assemblies
5:appdomain.currentdomain.setupinformation.applicationbase;//Gets or sets the name of the directory that contains the application
Get the root directory method of the Web application
1.httpcontext.current.server.mappath ("~/configs/channelusers.xml")
HttpContext.Current returns the HttpContext object for the current request. So we can directly access the request, Response, Session, application and other objects, and page access equivalent.
There are several ways to get the root directory of a Web site, such as:
Server.MapPath (request.servervariables["Path_info"])
Server.MapPath ("/")
Server.MapPath ("")
Server.MapPath (".")
Server.MapPath (".. /")
Server.MapPath ("..")
Page.Request.ApplicationPath
The above methods can be accessed in. aspx, but not if you are in the WinForm file.
HttpContext.Current.Server.MapPath ();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath can be used in. cs files.
But HttpContext.Current.Server.MapPath (); This gets the path to the file rather than the root directory.
Only System.Web.HttpContext.Current.Request.PhysicalApplicationPath this is the root directory to get, in the write get database path is should use this.
Determines whether a folder exists, does not exist, and creates
if (Directory.Exists (Server.MapPath (~/upimg/hufu)) = = false)//create the file folder if it does not exist {
Directory.CreateDirectory (Server.MapPath (~/upimg/hufu));}
Directory.delete (Server.MapPath (~/upimg/hufu), true);//delete folders and subdirectories in folders, files
Determine the existence of a file
if (File.exists (Server.MapPath (~/upimg/data.html))) {
Response.Write (Yes);//Presence File}else{
Response.Write (No);
File does not exist
File.create (MapPath (~/upimg/data.html));//Create the file}
String name = getfiles.filename;//Gets the name of the uploaded file
String size = GetFiles.PostedFile.ContentLength.ToString ();//Gets the size of the uploaded file
String type = getfiles.postedfile.contenttype;//Gets the mime of the uploaded file
string postfix = name. Substring (name. LastIndexOf (.) + 1);//Get the suffix of the uploaded file
String ipath = Server.MapPath (upimg) +\\+ name;//get the actual path to the file
String fpath = Server.MapPath (upfile) + \ \ + Name;
String Dpath = upimg\\ + name;//determine the virtual path to write to the database
Showpic.visible = true;//Active
Showtext.visible = true;//Active
Determine file format
if (name = =) {
Response.Write (<scriptalert (' upload file cannot be empty ') </script);} else{
if (postfix = = jpg | | postfix = = GIF | | postfix = = BMP | | postfix = = PNG) {
Getfiles.saveas (Ipath);
Showpic.imageurl = Dpath;
Showtext.text = The name of the image you uploaded is: + name + + File Size: + size + KB + + file type: + type + + Store The actual path is: + Ipath;} else{
showpic.visible = false;//Hidden pictures
Getfiles.saveas (Fpath);//Because it is not a picture file, so go to upfile this folder
Showtext.text = The file name you uploaded is: + name + + File Size: + size + KB + + file type: + type + + Store The actual path is: + Fpath;}
C # Gets the console application's root method to determine if the folder exists and does not exist to create