C # program made whether the root directory of the file exists and judgment

A: Get root method

Obtaining root console application method of
Method 1, Environment.CurrentDirectory fully qualified path to get or set the current working directory of the
method 2, AppDomain.CurrentDomain.BaseDirectory acquired directory group, which is solved by the assembly for detecting assembly procedure conflict

 

 

Obtaining the root WinForm application method
1, Environment.CurrentDirectory.ToString (); // get or set the current working directory of the fully qualified path
2, Application.StartupPath.ToString (); // Get an executable application initiated path to the file, not including the name of the executable file
3, Directory.GetCurrentDirectory (); // get the application's current working directory
4, AppDomain.CurrentDomain.BaseDirectory; // get base directory, it resolved by the assembly procedure conflict with to probe assembly
5, AppDomain.CurrentDomain.SetupInformation.ApplicationBase; // get or set the application directory contains a name

 

 

Get web application root directory methods

1.HttpContext.Current.Server.MapPath("~/configs/ChannelUsers.xml")

 

HttpContext.Current
Returns HttpContext object for the current request. So we can directly access the Request, Response, Session, Application and other objects, and access equivalent Page.
Page then we do not need to pass to our library objects with the way parameters.

HttpContext.Current.Session["name"] = "猪八戒";
string name = HttpContext.Current.Request.Param["name"];
HttpContext.Current.Response.Write("猪八戒好吃懒做!");

 

Get web root directory There are several ways such as:

Server.MapPath(Request.ServerVariables["PATH_INFO"])
Server.MapPath("/")
Server.MapPath("")
Server.MapPath(".")
Server.MapPath("../")
Server.MapPath("..") 
Page.Request.ApplicationPath
运行结果:
C:\Inetpub\wwwroot\EnglishClub\manage\WebForm1.aspx
C:\Inetpub\wwwroot\
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\
C:\Inetpub\wwwroot\EnglishClub

The above method can be accessed in the .aspx, but if you are. cs file can not be used.

 

HttpContext.Current.Server.MapPath();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath   

In .cs file can be used.

But HttpContext.Current.Server.MapPath (); This acquisition is a path to a file rather than the root.

This is the only System.Web.HttpContext.Current.Request.PhysicalApplicationPath taken root directory, write access to the database should use this path, there are other issues.

Reprinted from: http: //my.oschina.net/robortly/blog/685631

 

Two: to determine whether the files and folders methods exist

Copy the code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
 
public partial class Default3 : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
         ShowPic.Visible = false; // initializes not show
         ShowText.Visible = false; // initializes not show
     }
     protected void Button1_Click(object sender, EventArgs e)
     {
 
         if (Directory.Exists (Server.MapPath ( "~ / upimg / hufu")) == false) // If the file does not exist create folders
         {
             Directory.CreateDirectory(Server.MapPath("~/upimg/hufu"));
         }
 
         //Directory.Delete(Server.MapPath("~/upimg/hufu "), true); // delete folders, and folders in a subdirectory, file    
 
         // determine the existence of a file
 
         if (File.Exists(Server.MapPath("~/upimg/Data.html")))
         {
             Response.Write("Yes");
 
             // file exists
 
         }
 
         else
         {
             Response.Write("No");
             // File does not exist
             File.Create (MapPath ( "~ / upimg / Data.html")); // create the file
 
         }
 
         string name = GetFiles.FileName; // get the name of the uploaded file
         string size = GetFiles.PostedFile.ContentLength.ToString (); // get the size of uploaded files
         string type = GetFiles.PostedFile.ContentType; // get the uploaded file MIME
         string postfix = name.Substring (name.LastIndexOf () + 1 "."); // get the uploaded file suffix
         string ipath = Server.MapPath ( "upimg") + "\\" + name; // get the file the actual path
         string fpath = Server.MapPath("upfile") + "\\" + name;
         string dpath = "upimg \\" + name; // determines the virtual path into the database
 
         ShowPic.Visible = true; // activation
         ShowText.Visible = true; // activation
 
         // determine the file format
         if (name == "") { 
           Response.Write ( "<script> alert ( 'upload file can not empty') </ script>");
         }
 
         else{
 
             if (postfix == "jpg" || postfix == "gif" || postfix == "bmp" || postfix == "png")
             {
                 GetFiles.SaveAs(ipath);
                 ShowPic.ImageUrl = dpath;
                 ShowText.Text = "upload your image name is:" + name + "<br>" + "File size:" + size + "KB" + "<br>" + "Files of type:" + type + "< br> "+" stored in the actual path is: "+ ipath;
 
             }
 
             else
             {
                 ShowPic.Visible = false; // hide
                 GetFiles.SaveAs (fpath); // because it is not an image file, so there is upfile turn this folder
                 ShowText.Text = "file name you upload is:" + name + "<br>" + "File size:" + size + "KB" + "<br>" + "Files of type:" + type + "< br> "+" stored in the actual path is: "+ fpath;
 
             }
 
         
         }
 
 
     }
}

Copy the code

Reprinted from: http: //www.cnblogs.com/CookBlack/archive/2011/04/10/1883864.html

Guess you like

Origin blog.csdn.net/oshan2012/article/details/91821229