C # Determines if files and folders exist and is created (C # determines the folder exists)

Source: Internet
Author: User

protected void Button1_Click (object sender, EventArgs e)
{

if (Directory.Exists (Server.MapPath ("~/upimg/hufu")) = = false)//create a 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");

exists 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 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;//Active
Showtext.visible = true;//Active

Determine 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 image you uploaded is:" + name + "<br>" + "File Size:" + size + "KB" + "<br>" + "file type:" + type + "<br>" + "The actual path for storage 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 + "<br>" + "File Size:" + size + "KB" + "<br>" + "file type:" + type + "<br>" + "The actual path for storage is:" + fpath;

}
http://www.cnblogs.com/szytwo/archive/2012/03/22/2411703.html

C # Determines whether files and folders exist and is created (C # determines the folder exists)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.