c#中对文件的操作小结     1、建立一个文本文件 
    public class FileClass 
    { 
     public static void Main() 
     { 
     WriteToFile(); 
     } 
     static void WriteToFile() 
     { 
     StreamWriter SW; 
     SW=File.CreateText("c:\MyTextFile.txt"); 
     SW.WriteLine("God is greatest of them all"); 
     SW.WriteLine("This is second line"); 
     SW.Close(); 
     Console.WriteLine("File Created SucacessFully"); 
     } 
    } 
     
    2、读文件 
    public class FileClass 
    { 
     public static void Main() 
     { 
     ReadFromFile("c:\MyTextFile.txt"); 
     } 
     static void ReadFromFile(string filename) 
     { 
     StreamReader SR; 
     string S; 
     SR=File.OpenText(filename); 
     S=SR.ReadLine(); 
     while(S!=null) 
     { 
     Console.WriteLine(S); 
     S=SR.ReadLine(); 
     } 
     SR.Close(); 
     } 
    } 
     
    3、追加操作 
     
    public class FileClass 
    { 
     public static void Main() 
     { 
     AppendToFile(); 
     } 
     static void AppendToFile() 
     { 
     StreamWriter SW; 
     SW=File.AppendText("C:\MyTextFile.txt"); 
     SW.WriteLine("This Line Is Appended"); 
     SW.Close(); 
     Console.WriteLine("Text Appended Successfully"); 
     } 
    } 
    4、下载文件(From DataBase Read)
   
   int FileID=Convert.ToInt32(DataList_Event_AttachFile.DataKeys[e.Item.ItemIndex]);
 
   string sql="select FileName,File_Data,File_Type from AttachFile where FileID="+FileID;
   Amcham.Data d=new Amcham.Data();
   Amcham.Data.DataResult ds=new Amcham.Data.DataResult();
   ds=d.GetSearchResult(sql);
   if (ds.dr.Read())
    {     
    byte[] Buffer=new byte[ds.dr.GetSqlBinary(1).Length];
    ds.dr.GetBytes(1,0,Buffer,0,Buffer.Length);
    
    Response.ContentType=ds.dr.GetSqlString(2).ToString();
    Response.AddHeader("Content-Disposition", "attachment; filename=\"" + ds.dr.GetSqlString(0).ToString() + "\"");
    Response.BinaryWrite(Buffer);
    Response.End();//避免把页面HTML代码写入文件尾部
   5、读附件入数据库
FileStream=file.PostedFile.InputStream;
FileLen=file.PostedFile.ContentLength;
FileName=Path.GetFileName(file.PostedFile.FileName);
FileType=file.PostedFile.ContentType;
byte[] FileContent=new byte[FileLen];

FileStream.Read(FileContent,0,FileLen); AttachFile.FileName=FileName;
AttachFile.File_data=FileContent;
AttachFile.FileType=FileType;
AttachFile.EventID=EventID;
AttachFile.AddAttach();public class AttachFile
{
public System.Data.SqlTypes.SqlInt32 AttachID;
public System.Data.SqlTypes.SqlInt32 EventID;
public System.Data.SqlTypes.SqlInt32 SpeakerID;
public System.Data.SqlTypes.SqlString FileName;
public System.Data.SqlTypes.SqlString FileType;
public byte[] File_data;
public  int AddAttach()
{
string InsertSql="INSERT INTO AttachFile(FileID, EventID, SpeakerID, FileName,File_Type, File_data) VALUES (@" +
"FileID, @EventID, @SpeakerID, @FileName,@FileType, @File_data)";
SqlConnection myConnection = new  SqlConnection(amcham.Data.ConDB.ConnectionString);
SqlCommand myCommand = new  SqlCommand(InsertSql, myConnection);
// Add Parameters
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileID", System.Data.SqlDbType.Int));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@EventID", System.Data.SqlDbType.Int));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SpeakerID", System.Data.SqlDbType.Int));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileName", System.Data.SqlDbType.VarChar, 50));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@File_data", System.Data.SqlDbType.Image));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileType", System.Data.SqlDbType.VarChar, 50));

// Add Value
this.AttachID=this.GetNewAttachID();
myCommand.Parameters["@FileID"].Value=this.AttachID;
myCommand.Parameters["@EventID"].Value=this.EventID;
myCommand.Parameters["@SpeakerID"].Value=this.SpeakerID;
myCommand.Parameters["@FileName"].Value=this.FileName;
myCommand.Parameters["@File_data"].Value=this.File_data;
myCommand.Parameters["@FileType"].Value=this.FileType; // Execute the command
try 
{
// Open the connection and execute the Command
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
return (int)(this.AttachID);
}
catch (Exception e)
{
// An error occurred, pass the exception up  Event

throw e;
} }