Summary (5) - File Operations

xiaoxiao2021-03-17  179

Summary (5) - File Operations

Squire: Terrylee

One. Read text file

1

/ ** /

///

2 /// Read text file 3 ///

4

Private

Void

ReadfromtxtFile ()

5

{6 if (! FilePath.PostedFile.FileName = "") 7 {8 txtFilePath = filePath.PostedFile.FileName; 9 fileExtName = txtFilePath.Substring (txtFilePath.LastIndexOf ( "") 1,3.); 1011 if (fileExtName = "TXT" && fileextName! = "TXT") 12 {13 response.write ("Please select text file"); 14} 15 else16 {17 StreamReader filestream = New StreamReader (txtFilepath, Encoding.default); 18 txtcontent. Text = filestream.readToend (); 19 filestream.close (); 20} 21} 22}

two. Get a list of files

1

/ ** /

///

2 /// Get file list 3 ///

4

Private

Void

GetFileList ()

5

{6 string strcurdir, filename, fileext; 7 8 / ** //// file size 9 long filesis; 10 11 / ** //// final modification time; 12 datetime filemodify; 13 14 / ** //// Initializing 15 if (! Ispostback) 16 {17 / ** //// Initialization, the default is the directory of the current page 18 strcurdir = server.mappath ("."); 19 lblcurdir.text = stracurdir; 20 txtcurdir.text = strCurDir; 21} 22 else 23 {24 strCurDir = txtCurDir.Text; 25 txtCurDir.Text = strCurDir; 26 lblCurDir.Text = strCurDir; 27} 28 FileInfo fi; 29 DirectoryInfo dir; 30 TableCell td; 31 TableRow tr; 32 tr = New tableerow (); 33 34 / ** //// Dynamic Add cell content 35 TD = new Tablecell (); 36 TD.Controls.Add (New LiteralControl ("File Name)); 37 Tr.Cells. Add (TD); 38 TD = New Tablecell (); 39 TD.Controls.add (New LitralControl ("File Type")); 40 Tr.Cells.Add (TD); 41 TD = New Tablecell (); 42 TD .Controls.add (New LiteralControl ("file size")); 43 tr.cells.add (TD); 44 TD = new Tablecell (); 45 TD.Controls.Add (New L Iteralcontrol ("final modification time")); 46 tr.cells.add (TD); 47 48 TableDirinfo.Rows.Add (TR); 49 50 / ** ////> Configure directory reference object 51 DirectoryInfo Dirinfo = New DirectoryInfo (txtcurdir.text); 52 53 / ** //// Cycle to determine the files and directories in the current directory 54 foreach (filesysteminfo fsi in dirinfo.getfilesysteminfos ()) 55 {56 filename = ""; 57 fileext = ""; 58 filesize = 0; 59 60 / ** ///// If it is a file 61 if (fsi is fileinfo) 62 {63 fi = (fileInfo) fsi; 64 65 / ** //// obtained file name 66 FILENAME = FI.NAME; 67 68 / ** //// Optical extension 69 fileext =

Fi.extension; 70 71 / ** //// Number 72 filesize = FI.LENGTH; 73 74 / ** //// Number of files to achieve the final modification time of 5 FileModify = FI.lastwrittime; 76} 77 78 / ** /// / Otherwise, the directory 79 else 80 {81 DIR = (DirectoryInfo) fsi; 82 83 / ** //// get a directory name 84 filename = dir.name; 85 86 / ** //// The last modification time of the catalog 87 filemodify = dir.lastwrittime; 88 89 / ** //// Setting file extension is "folder" 90 fileext = "folder"; 91} 92 93 / ** /// / Dynamic Add Table Content 94 Tr = New TableRow (); 95 TD = New Tablecell (); 96 Td.Controls.Add (New Litralcontrol (FileName)); 97 Tr.Cells.Add (TD); 98 TD = New Tablecell (); 99 td.control (new literalcontrol (fileext)); 100 tr.cells.add (TD); 101 TD = new TableCell (); 102 Td.controls.add (new literalcontrol (filesis.toString () "Byte")); 103 tr.cells.add (TD); 104 td = new TableCell (); 105 td.Controls.Add (new LiteralControl (FileModify.ToString ( "yyyy-mm-dd hh: mm: ss"))); 106 tr.Cells.Add (td); 107 tableDirInfo. Rows.Add (TR); 108} 109}. Read the log file

1

/ ** /

///

2 /// Read log file 3 ///

4

Private

Void

Readlogfile ()

5

{6 / ** //// read log files from the specified directory to open or create the log file 7 FILESTREAM FS = New FileStream (Server.MAppath ("UPEDFILE") "// logfile.txt", FileMode.Openorcreate , FileAccess.read; 8 9 / ** //// Define Output String 10 StringBuilder Output = New StringBuilder (); 11 12 / ** //// Initialization The length of the string is 013 Output.length = 0 14 15 / ** /// / Create a read data stream for the file stream created above 16 StreamReader Read = New StreamReader (FS); 17 18 / ** //// Set the starting position of the current stream is the file stream The starting point 19 read.basestream.seek (0, seekorigin.begin); 20 21 / ** //// Read file 22 While (read.peek ()> -1) 23 {24 / ** //// / Take the content of the file and wrap 25 output.append (read.readline () "/ n"); 26} 27 28 / ** /// Off Release Data Stream 29 Read.Close (); 30 31 / ** //// Remove the read log file content 32 return output.toString (); 33} IV. Write log file

1

/ ** /

///

2 /// Write log file 3 /// 4 ///

5

Private

Void

WriteLogfile

String

INPUT

6

{7 / ** //// Specify the directory of the log file 8 string fname = server.mappath ("UPEDFILE") "//logfile.txt"; 9 / ** //// Define File Information Object 10 FileInfo Finfo = New fileInfo (FNAME); 1112 / ** //// Decision file is existed and whether it is greater than 2k13 if (Finfo.exists && Finfo.Length) 14 {15 / ** //// Delete this file 16 FINFO . Delte (); 17} 18 / ** /// Create only write file stream 19 using (filestream fs = Finfo.openwrite ()) 20 {21 / ** /// / / According to the file created according to the files created above Data stream 22 streamWriter W = New StreamWriter (fs); 23 24 / ** //// Set the starting position of the write data to the end of the file stream 25 w.basestream.seek (0, seekorigin.end); 26 27 / ** //// Write "log entry:" 28 w.write ("/ nlog entry:"); 29 30 / ** //// Write the current system time and wrap 31 w.write ("{" 0} {1} / r / n ", DateTime.now.tolongTimeString (), 32 datetime.now.tolongdateString ()); 33 34 / ** //// Write log content and wrap 35 w.write (Input) "/ N"); 36 37 / ** //// write ------------------------------ ---- "and wrap 38 w.write (" --------------------------------- / N "); 39 40 / ** //// Clear The contents of the buffer are written to the base stream 41 w.flush (); 42 43 / ** //// Close the write data stream 44 w.close (); 45} 46}. Create an HTML file

1

/ ** /

///

2 /// Create an HTML file 3 ///

4

Private

Void

Createhtmlfile ()

5

{6 / ** //// Definition and HTML tag number of array 7 string [] newcontent = new string [5]; 8 StringBuilder strHtml = new stringbuilder (); 9 try 10 {11 / ** ///// Create a StreamReader object 12 Using (StreetReader sr = new streamreader (Server.MAppath ("createhtml") "//Template.html") 13 {14 string oneLine; 15 16 / ** /// read the specified HTML File Template 17 While ((OneLine = sr.readline ())! = Null) 18 {19 strHtml.Append (OneLine); 20} 21 Sr.Close (); 22} 23} 24 Catch (Exception Err) 25 {26 / ** //// Output exception information 27 response.write (Err.tostring ()); 28} 29 / ** //// is assigning a value of 30 newcontent [0] = txtttitle.text; // Title 31 NewContent [1] = "Backcolor = '# cccfff'"; // background color 32 newcontent [2] = "# ff0000"; // Font color 33 newcontent [3] = "100px"; // Font size 34 newcontent [ 4] = txtcontent.text; // Main content 3536 / ** //// / / According to the new content above, the HTML file 37 TRY38 {39 / ** //// specifies the HTML file 40 string fname = Server. Mappath ("CreateHTML") "//" datetime.now.tostring ("YYYYMMDDHMMMSS") ".html"; 41 42 / ** /// Replace the marked in the HTML template file 43 for (INT i = 0; i <5; i ) 44 {45 strHtml.Replace ("$ HTMLKEY [" i "]", newcontent [i]); 46} 47 / ** //// creation file information object 48 fileinfo finfo = new fileinfo FNAME); 49 50 / ** //// Create a file stream in the form of open or write 51 using (filestream fs = finfo.openwrite ()) 52 {53 / ** /// / / According to the file stream created above Create a write data stream 54 streamwriter sw = new streamwriter (fs, system.text.Encoding.getencoding ("GB2312"));

转载请注明原文地址:https://www.9cbs.com/read-129652.html

New Post(0)