Summary of C Basic Methods of Operating Local Files and Saving Files to Database

  • 2021-10-11 19:20:47
  • OfStack

Namespace:


using System.IO;

Write a text file


StreamWriter sw=File.CreateText("c:\\text.txt");  
sw.WriteLine("C#");                // Write 1 Line text  
sw.Write("www.csdn.net");             // Write text at the end of text  
sw.Flush();                    // Empty  
sw.Close();                    // Shut down  

Read a text file


StreamReader sr = File.OpenText("c:\\text.txt"); 
sr.ReadLine();                  // Read 1 Row data  
sr.Read();                    // Read 1 Characters  
sr.ReadToEnd();                  // Read from the current position to the end of the text  
sr.Close();                    // Release resources  

Append text file


StreamWriter sw = File.AppendText("c:\\text.txt"); 
sw.WriteLine("C#");                 // Write 1 Line text  
sw.Write("www.csdn.net");              // Write text at the end of text  
sw.Flush();                     // Empty  
sw.Close();                     // Shut down  

Determine whether the file exists


File.Exists("c:\\text.txt"); 

Delete a file


File.Delete("c:\\text.txt"); 

Copy a file


File.Copy("c:\\text.txt", "c:\\copy.txt");   // Put c:\\text.txt Copy to c:\\copy.txt 

Move a file


File.Copy("c:\\text.txt", "d:\\text.txt");  // Put c:\\text.txt Move to d:\\text.txt 

Folder creation, movement, deletion


Directory.Delete("c:\\test");       // Delete C Under the disk test Folder  
Directory.CreateDirectory("c:\\test");  // In C Disk creation test Folder  
Directory.Exists("c:\\test");       // Validation C Disk test Does the folder exist  
Directory.Move("c:\\test", "d:\\test");  // Put c:\test Move to d:\test 

Save files in Oracle database (C #)
Blob and Clob in Oracle can hold large amounts of data. Among them, Blob refers to binary large object, which is the abbreviation of English Binary Large Object, and is used to store a large amount of binary data. Clob refers to the large character object, which is the abbreviation of English Character Large Object, and is used to store a large amount of text data.
1. Database tables

--Create a file table


create table tb_file 
( 
 id       number(20) not null, 
 file_name    nvarchar2(100), 
 file_content  blob, 
 constraint pk_tb_file primary key (id) 
) 
tablespace mydb storage( 
 initial 64K 
 minextents 1 
 maxextents unlimited 
); 


--Set tb_file primary key autoincrement


StreamWriter sw=File.CreateText("c:\\text.txt");  
sw.WriteLine("C#");                // Write 1 Line text  
sw.Write("www.csdn.net");             // Write text at the end of text  
sw.Flush();                    // Empty  
sw.Close();                    // Shut down  

0

2 Save and read binary files in database


StreamWriter sw=File.CreateText("c:\\text.txt");  
sw.WriteLine("C#");                // Write 1 Line text  
sw.Write("www.csdn.net");             // Write text at the end of text  
sw.Flush();                    // Empty  
sw.Close();                    // Shut down  

1


Related articles: