C # determine whether a file or folder exists and is created

Source: Internet
Author: User

C # application knowledge collection 14:19:57564Comment0 Font Size:LargeMediumSmall

C # determine whether a file or folder exists and is created

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 is not displayed

Showtext. Visible = false; // Initialization is not displayed

}

Protected void button#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, subdirectories, and files in folders.

// Determine the existence of the file

If (file. exists (server. mappath ("~ /Upimg/data.html ")))

{

Response. Write ("yes ");

// File exists

}

Else

{

Response. Write ("no ");

// The 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 (); // obtain the size of the uploaded file.

String type = getfiles. postedfile. contenttype; // obtain the mime of the uploaded file

String Postfix = Name. substring (name. lastindexof (".") + 1); // obtain the suffix of the uploaded file

String ipath = server. mappath ("upimg") + "\" + name; // obtain the actual file path

String fpath = server. mappath ("upfile") + "\" + name;

String dpath = "upimg \" + name; // determines the virtual path written to the database

Showpic. Visible = true; // Activation

Showtext. Visible = true; // Activation

// Determine the file format

If (name = ""){

Response. Write ("<SCRIPT> alert ('file cannot be blank ') </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> "+" actual storage path: "+ ipath;

}

Else

{

Showpic. Visible = false; // hide the image

Getfiles. saveas (fpath); // The folder upfile is stored because it is not an image file.

Showtext. TEXT = "the name of the file you uploaded is:" + name + "<br>" + "file size: "+ size +" kb "+" <br> "+" file type: "+ Type +" <br> "+" actual storage path: "+ fpath;

}

}

}

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.