C # Determines if files and folders exist and creates

Source: Internet
Author: User

C # Determines if files and folders exist and creates

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;//initialization does not appear
Showtext.visible = false;//initialization does not appear
}
protected void Button1_Click (object sender, EventArgs e)
{

if (Directory.Exists (Server.MapPath ("~/upimg/hufu")) = = false)//if not saved
When you create the file folder
{
Directory.CreateDirectory (Server.MapPath ("~/upimg/hufu"));
}

Directory.delete (Server.MapPath ("~/upimg/hufu"), true);//delete folder to
and the subdirectory files in the folder

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 ();//Get Uploaded
Size of File
String type = getfiles.postedfile.contenttype;//Gets the mime of the uploaded file
string postfix = name. Substring (name. LastIndexOf (".") + 1);//Get Uploaded
Suffix of 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 large
Small: "+ size +" KB "+" <br> "+" file type: "+ type +" <br> "+" to store the actual path is: "+
Ipath

}

Else
{
showpic.visible = false;//Hidden pictures
Getfiles.saveas (Fpath);//Because it is not a picture file, so it exists upfile this
Folder
Showtext.text = "The file name you uploaded is:" + name + "<br>" + "file large

Small: "+ size +" KB "+" <br> "+" file type: "+ type +" <br> "+" to store the actual path is: "+
Fpath;
}
}
}
}

C # Determines if files and folders exist and creates

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.