命名空间:
using System.IO;
写文本文件
StreamWriter sw=File.CreateText("c:text.txt"); sw.WriteLine("C#"); //写入一行文本 sw.Write("www.csdn.net"); //在文本末尾写入文本 sw.Flush(); //清空 sw.Close(); //关闭
读文本文件
StreamReader sr = File.OpenText("c:text.txt"); sr.ReadLine(); //读取一行数据 sr.Read(); //读取一个字符 sr.ReadToEnd(); //从当前位置读取到文本结束 sr.Close(); //释放资源
追加文本文件
StreamWriter sw = File.AppendText("c:text.txt"); sw.WriteLine("C#"); //写入一行文本 sw.Write("www.csdn.net"); //在文本末尾写入文本 sw.Flush(); //清空 sw.Close(); //关闭
判断文件是否存在
File.Exists("c:text.txt");
删除文件
File.Delete("c:text.txt");
复制文件
File.Copy("c:text.txt", "c:copy.txt"); //把c:text.txt复制到c:copy.txt
移动文件
File.Copy("c:text.txt", "d:text.txt"); //把c:text.txt移动到d:text.txt
文件夹创建、移动、删除
Directory.Delete("c:test"); //删除C盘下的test文件夹 Directory.CreateDirectory("c:test"); //在C盘创建test文件夹 Directory.Exists("c:test"); //验证C盘test文件夹是否存在 Directory.Move("c:test", "d:test"); //把c:test移动到d:test
Oracle数据库中保存文件(C#)
Oracle中有Blob和Clob可以保存大数据量。其中Blob是指二进制大对象也就是英文Binary Large Object的缩写,用来存储大量二进制数据。而Clob是指大字符对象是英文Character Large Object的缩写,用来存储大量文本数据。
1.数据库表
-- 创建文件表
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 );
--设置tb_file主键自增
create sequence seq_tb_file --创建自增序列 minvalue 1 maxvalue 9999999999999999999 start with 1 increment by 1 nocache order; create or replace trigger ai_tb_file --创建触发器,当插入新记录时自增主键id before insert on tb_file for each row when (new.id is null) begin select seq_tb_file.nextval into :new.id from dual; end;
2 在数据库中保存和读取二进制文件
//添加命名空间 using System; using System.Data.OracleClient; using System.IO; using System.Data; /// <summary> /// 把文件插入数据库中 /// </summary> /// <param name="filePath">文件名,包含路径,如c:temp.rar</param> /// <returns></returns> public int Insert(string filePath) { string connStr = "data source=orcl;user=mydbuser;password=mydbuser;"; OracleConnection conn = new OracleConnection(connStr); OracleCommand cmd = conn.CreateCommand(); //读取文件 FileStream fs = File.OpenRead(filePath); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); OracleParameter paramFileContent = cmd.CreateParameter(); paramFileContent.OracleType = OracleType.Blob; paramFileContent.ParameterName = "FileContent"; paramFileContent.Direction = ParameterDirection.Input; paramFileContent.Value = buffer; cmd.Parameters.Add(paramFileContent); OracleParameter paramFileName = cmd.CreateParameter(); paramFileName.OracleType = OracleType.VarChar; paramFileName.ParameterName = "FileName"; paramFileName.Direction = ParameterDirection.Input; paramFileName.Value = Path.GetFileName(filePath); cmd.Parameters.Add(paramFileName); string sqlInsert = "insert into tb_file (file_name, file_content) values (:FileName, :FileContent)"; cmd.CommandText = sqlInsert; cmd.CommandType = CommandType.Text; conn.Open(); int result = cmd.ExecuteNonQuery(); conn.Close(); return result; } /// <summary> /// 根据文件名从数据库中获取文件 /// </summary> /// <param name="fileName">数据库中的文件名</param> /// <param name="savePath">文件的保存路径,包括文件名,如c:file.rar</param> public void Select(string fileName, string savePath) { string connStr = "data source=orcl;user=mydbuser;password=mydbuser;"; OracleConnection conn = new OracleConnection(connStr); OracleCommand cmd = conn.CreateCommand(); string sqlSelect = "select file_name, file_content from tb_file where file_name=:FileName"; cmd.CommandText = sqlSelect; cmd.CommandType = CommandType.Text; OracleParameter paramFileName = cmd.CreateParameter(); paramFileName.OracleType = OracleType.VarChar; paramFileName.ParameterName = "FileName"; paramFileName.Direction = ParameterDirection.Input; paramFileName.Value = fileName; cmd.Parameters.Add(paramFileName); conn.Open(); OracleDataReader dr = cmd.ExecuteReader(); dr.Read(); byte[] buffer = (byte[])dr["file_content"]; dr.Close(); conn.Close(); //把文件保存到指定路径 File.WriteAllBytes(savePath, buffer); }