ASP. NET File Operations

Source: Internet
Author: User

Using system;
Using system. collections;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using system. IO;
Using system. collections;

Public partial class anewfile: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}
Protected void btnewfile_click (Object sender, eventargs E)
{
If (this. txfilename. Text = string. Empty)
{
Response. Write ("<SCRIPT> alert ('Enter the name') </SCRIPT> ");
}
Else
{
String name = This. txfilename. text;
String Path = server. mappath ("") + "\" + name; // obtain the path of the folder Server
If (directory. exists (PATH) // determines whether the folder exists on the server.
{
Response. Write ("<SCRIPT> alert ('Folder already exists ') </SCRIPT> ");
}
Else
{
Directoryinfo folder = directory. createdirectory (PATH); // create a file
String time = convert. tostring (directory. getcreationtime (PATH); // obtain the creation time
String Foldername = This. txfilename. Text. substring (this. txfilename. Text. lastindexof ("\") + 1 );
Response. Write ("<SCRIPT> alert ('created successfully" + Foldername + "time =" + time + "') </SCRIPT> ");
}
}
}
Protected void button#click (Object sender, eventargs E)
{
If (this. textbox1.text = string. Empty)
{
Response. Write ("Enter the name ");
}
Else
{
String name = This. textbox1.text;
String deletepath = server. mappath ("") + "\" + name;
If (directory. exists (deletepath ))
{
Directory. Delete (deletepath );
Response. Write ("<SCRIPT> alert ('deleted successfully! ') </SCRIPT> ");
}
Else
{
Response. Write ("this folder is not found ");
}
}
}
Protected void button2_click (Object sender, eventargs E)
{
String pathname = This. textbox2.text;
String filename = server. mappath ("") + "//" + pathname;
This. listbox1.items. Clear ();
If (! Directory. exists (filename ))
{
Listbox1.visible = false;
Response. Write ("<SCRIPT> alert ('Folder not found ') <SCRIPT> ");
}
Else
{
Directoryinfo foldinfo = new directoryinfo (filename );
Filesysteminfo [] dirs = foldinfo. getfilesysteminfos (); // The getfilesysteminfos method obtains the list in the folder and saves the data to the filesysteminfo [] array.
If (dirs. Length <1) // determine whether the folder contains data
{
Response. Write ("<SCRIPT> alert ('" + foldinfo + "no data in the file') <SCRIPT> ");
Return;
}
This. listbox1.visible = true;
This. listbox1.datasource = dirs; // bind to ListBox
This. listbox1.databind ();
}
}
Protected void button3_click (Object sender, eventargs E)
{
String pathname = This. textbox3.text;
String filename = server. mappath ("") + "\" + pathname;
If (! File. exists (filename) // determines whether the file exists.
{
Response. Write ("<SCRIPT> alert ('this file does not exist') </SCRIPT> ");
Return;
}
Try
{
File. Delete (filename); // Delete
Response. Write ("<SCRIPT> alert ('deleted successfully') </SCRIPT> ");
}
Catch (exception ERR)
{
Response. Write (ERR. tostring ());
}
}
Protected void button4_click (Object sender, eventargs E)
{
String pathname = This. textbox4.text;
String filename = server. mappath ("") + "\" + pathname;
If (! File. exists (filename) // determines whether the file exists.
{
Response. Write ("<SCRIPT> alert ('this file does not exist') </SCRIPT> ");
Return;
}
Else
{
This. panel1.visible = true;
Fileinfo file = new fileinfo (filename );
This. label1.text = file. fullname; // obtain the file path
This. label2.text = file. Length + "Byte"; // obtain the file size.
This. label3.text = file. Attributes. tostring (); // get file attributes
This. label4.text = file. creationtime. tow.datestring (); // obtain the creation time.
This. label5.text = file. lastaccesstime. to1_datestring (); // time of last access
}
}
Protected void button5_click (Object sender, eventargs E)
{
String pathname = This. fileupload1.postedfile. filename;
Fileinfo Fi = new fileinfo (pathname); // create a file operation fileinfo object
String filename = Fi. Name; // get the file name
String extension = Fi. extension; // get the file extension
If (extension! = ". Txt ")
{
Response. Write ("<SCRIPT> alert ('Open TXT document') </SCRIPT> ");
Return;
}
Streamreader sr = new streamreader (pathname, system. Text. encoding. Default); // create a data stream to read the streamreader object and set its read encoding method
This. label6.text = filename + "the content in the file is as follows ";
This. label7.text = "& nbsp;" + Sr. readtoend (); // display content
Sr. Close ();

}
Protected void button6_click (Object sender, eventargs E)
{
String Path = This. fileupload2.postedfile. filename. tostring ();
If (Path = string. Empty)
{
Response. Write ("<SCRIPT> alert ('Open the document to modify ') </SCRIPT> ");
Return;
}
Fileinfo info = new fileinfo (PATH );
String Extentsion = info. extension. tostring (); // get the file extension
If (Extentsion! = ". Txt ")
{
Response. Write ("<SCRIPT> alert ('Open TXT document') </SCRIPT> ");
Return;
}
Viewstate ["path"] = path;
Streamreader sr = new streamreader (path, system. Text. encoding. Default); // create a data stream to read the streamreader object and set its read encoding method
This. textbox5.text = Sr. readtoend (); // display content
Sr. Close ();
}
Protected void button7_click (Object sender, eventargs E)
{
Try
{
String Path = viewstate ["path"]. tostring ();
Streamwriter RW = new streamwriter (path, true, system. Text. encoding. getencoding ("UTF-8 "));
RW. writeline (this. textbox5.text );
RW. Close ();
This. textbox5.text = string. empty;
Response. Write ("<SCRIPT> alert ('data written successfully') </SCRIPT> ");
Viewstate ["path"] = NULL;
}
Catch
{
Response. Write ("<SCRIPT> alert ('select text') </SCRIPT> ");
}
}
}

Related Article

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.