ASP. Net file Operation Small example (create folder, read, write, delete)

Source: Internet
Author: User
Tags add time

Static build to create a folder in the virtual directory to save the generated page then you need to manipulate the file. Create folder using System.IO;        String name = "AA";        String path = Server.MapPath ("") + "\ \" + Name; if (directory.exists (path)) {Response.Write (' <script>alert (' folder already exists! ');        History.go ( -1);</script> ");        } else {DirectoryInfo folder=directory.createdirectory (path);        String time = convert.tostring (directory.getcreationtime (path)); String foldername = name. Substring (name.        LastIndexOf ("\ \") + 1);        Response.Write ("Add success!");        Response.Write ("Add Time:" +time);        Response.Write ("folder name:" +foldername);        } Two, delete folder using System.IO;        String name = "AA";        String path = Server.MapPath ("") + "\ \" + Name;            if (directory.exists (path)) {directory.delete (path);        Response.Write ("Delete succeeded!"); } else {Response.Write (' <script>alert (' folder does not exist! ');        History.go ( -1);</script> "); } third, move the folderMoving string name1 = "AA";         String name2 = "Bb\\aa";        Move to the folder, move AA to bb string path1 = Server.MapPath ("") + "\ \" + name1;        String path2 = Server.MapPath ("") + "\ \" + name2; if (! Directory.Exists (path1)) {Response.Write (' <script>alert (' folder ' +name1+ ' does not exist! ');            History.go ( -1);</script> ");        Return } if (Directory.Exists (path2)) {Response.Write ("<script>alert" (' folder ' + name2 + ' already exists! ');            History.go ( -1);</script> ");        Return            } try {directory.move (path1, path2);        Response.Write ("folder moved successfully!"); } catch {Response.Write (' <script>alert (' must operate in the same directory! ');        History.go ( -1);</script> "); Get a list of files under folder foreground <asp:listbox id= "list" runat= "Server" width= "200px" height= "300px" visible= "false" ></asp:        listbox> background String name = "AA"; String path = Server.MapPath ("") + "\ \" + Name; if (! Directory.Exists (path)) {list.            Visible = false; Response.Write ("<script>alert" (' folder ' + ' name + ' does not exist! ');            History.go ( -1);</script> ");        Return            } else {DirectoryInfo foldinfo = new DirectoryInfo (path); filesysteminfo[] dirs = Foldinfo.            Getfilesysteminfos (); if (dirs. Length < 1) {Response.Write ("<script>alert (' No data in folder! ');                History.go ( -1);</script> ");            Return } list.            Visible = true; List.            DataSource = dirs; List.        DataBind ();        } v. Create a file String name = "Aa.aspx";        String path = Server.MapPath ("") + "\ \" + Name; if (file.exists (path)) {Response.Write ("<script>alert" (' file ' + name + ' already exists! ');            History.go ( -1);</script> ");        Return                  } else {FileStream fs = file.create (path);   Fs.            Close ();        Response.Write ("file" + name + "Add success!");        VI. copy file string name1 = "aa\\1.html";        String name2 = "bb\\1.html";        String path1 = Server.MapPath ("") + "\ \" + name1;        String path2 = Server.MapPath ("") + "\ \" + name2; if (! File.exists (path1)) {Response.Write ("<script>alert" ("file" + name1 + "does not exist!");            History.go ( -1);</script> ");        Return } if (File.exists (path2)) {Response.Write ("<script>alert" (' file ' + name2 + ' already exists! ');            History.go ( -1);</script> ");        Return            } try {file.copy (path1, path2);        Response.Write ("Copy succeeded!");        } catch {Response.Write ("Copy failed!");        Move file String name1 = "aa\\1.html";        String name2 = "bb\\1.html";        String path1 = Server.MapPath ("") + "\ \" + name1;        String path2 = Server.MapPath ("") + "\ \" + name2; if (! File.exists (path1))        {Response.Write ("<script>alert" (' file ' + name1 + ' does not exist! ');            History.go ( -1);</script> ");        Return } if (File.exists (path2)) {Response.Write ("<script>alert" (' file ' + name2 + ' already exists! ');            History.go ( -1);</script> ");        Return            } try {file.move (path1, path2);        Response.Write ("Mobile success!");        } catch {Response.Write ("Move failed!");      } viii. File Delete string name = "Bb\\1.html";             String path = Server.MapPath ("") + "\ \" + Name; if (! File.exists (Path)) {Response.Write (' <script>alert (' file ' + ' name + ' does not exist! ');            History.go ( -1);</script> ");        Return            } try {file.delete (path);        Response.Write ("Delete succeeded!");        } catch {Response.Write ("Delete failed!");      } Nine, get the details of the file string name = "Aa\\11.txt"; String path = Server.MapPath ("") + "\ \" + NAme if (! File.exists (Path)) {Response.Write (' <script>alert (' file ' + ' name + ' does not exist! ');          History.go ( -1);</script> ");      Return          } else {FileInfo file = new FileInfo (path);          String filexinxi1, Filexinxi2, Filexinxi3, Filexinxi4, filexinxi5; File path filexinxi1 = files.          FullName; File size, byte Filexinxi2 = files.          length+ "bytes"; File properties Filexinxi3 = files.          Attributes.tostring (); File creation Time Filexinxi4 = files.          Creationtime.toshortdatestring (); Last access Time Filexinxi5 = file.          Lastaccesstime.toshortdatestring ();          Response.Write ("File path:" +filexinxi1+ "<br>");          Response.Write ("File size:" + filexinxi2 + "<br>");          Response.Write ("File attributes:" + filexinxi3 + "<br>");          Response.Write ("File creation time:" + filexinxi4 + "<br>");      Response.Write ("File Last access time:" + filexinxi5 + "<br>"); } Ten, read the file contents string name = "Aa\\11.html";      String path = Server.MapPath ("") + "\ \" + Name;      FileInfo fi = new FileInfo (path); Gets the file name string filename = fi.        Name; Gets the file name extension string extension = fi.        Extension; Determine if the file is in the TXT format if (extension! = ". html") {Response.Write ("<script>alert (' Please select HTML format file! ');          History.go ( -1);</script> ");      Return      } StreamReader sr = new StreamReader (path, System.Text.Encoding.Default);      Response.Write ("The contents of the document are as follows:<br>"); Response.Write (Sr.      ReadToEnd ()); Sr.        Close (); Xi. write to file string name = "Aa\\11.html";      String content1= "  

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.