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;
}
}
}