C implements the method of determining whether a folder exists or not and creating a folder

  • 2021-12-04 19:26:33
  • OfStack

This article illustrates how the C # implementation determines whether a folder exists or not and creates a folder. Share it for your reference, as follows:


protected void Button1_Click(object sender, EventArgs e)
{
   if (Directory.Exists(Server.MapPath("~/upimg/hufu")) == false)// Create if it does not exist file Folder 
   {
    Directory.CreateDirectory(Server.MapPath("~/upimg/hufu"));
   }
   //Directory.Delete(Server.MapPath("~/upimg/hufu"), true);// Delete folders and subdirectories and files in folders 
   // Determine the existence of a file 
   if (File.Exists(Server.MapPath("~/upimg/Data.html")))
   {
    Response.Write("Yes");
    // Existent file 
   }
   else
   {
    Response.Write("No");
    // No file exists 
    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();// Gets the size of the uploaded file 
   string type = GetFiles.PostedFile.ContentType;// Object of the uploaded file MIME
   string postfix = name.Substring(name.LastIndexOf(".") + 1);// Get the suffix of the uploaded file 
   string ipath = Server.MapPath("upimg") +"\\"+ name;// Get the actual path of the file 
   string fpath = Server.MapPath("upfile") + "\\" + name;
   string dpath = "upimg\\" + name;// Determine the virtual path to write to the database 
   ShowPic.Visible = true;// Activate 
   ShowText.Visible = true;// Activate 
   // Determine the file format 
   if (name == "") {
   Response.Write("<script>alert(' 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 picture you uploaded is :" + name + "<br>" + " File size :" + size + "KB" + "<br>" + " File type :" + type + "<br>" + " The actual path to store is :" + ipath;
    }
    else
    {
     ShowPic.Visible = false;// Hide picture 
     GetFiles.SaveAs(fpath);// Because it is not a picture file , Therefore, it exists upfile This folder 
     ShowText.Text = " The name of the file you uploaded is :" + name + "<br>" + " File size :" + size + "KB" + "<br>" + " File type :" + type + "<br>" + " The actual path to store is :" + fpath;
    }
   }
}

More readers interested in C # can check the topic of this site: "C # File Operation Skills Summary", "C # Traversal Algorithm and Skills Summary", "C # Programming Thread Use Skills Summary", "C # Operating Excel Skills Summary", "XML File Operation Skills Summary in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: