c#中对文件的操作小结
墨初 知识笔记 120阅读
c #中对文件的操作小结1、建立一个文本文件公共类{ publistativoidmain(){写入文件();} staticvoidWriteToFile(){ StreamWriterSW;SW=(' c : \我的文字');西南WriteLine(' Godisgreatestofthemall ');西南WriteLine(' thisisecondline ');西南. close();控制台WriteLine(" ");}}2、读文件public class { publicstaticvoidMain()
1)">{ReadFromFile("c:\MyText");
}
static void ReadFromFile(string )
{
StreamReader SR;
string S;
SR=();
S=SR.ReadLine();
while(S!=null)
{
Console.WriteLine(S);
S=SR.ReadLine();
}
SR.Close();
}
}
3、追加操作
public class ;
{
public static void Main()
{
AppendToFile();
}
static void AppendToFile()
{
StreamWriter SW;
SW=("C:\MyText");
SW.WriteLine("This Line Is Appended");
SW.Close();
Console.WriteLine("Text Appended Successfully");
}
}
4、下载文件(From DataBase Read)
int FileID=Convert.ToInt32(DataList_Event_Attach[e.Item.ItemIndex]);
string sql="select "+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; \"" + ds.dr.GetSqlString(0).ToString() + "\"");
Response.BinaryWrite(Buffer);
Response.End();//避免把页面HTML代码写入文件尾部
5、读附件入数据库
=;
=;
=Path.Get();
=;
byte[] =new byte[];
(,0,);
Attach=;
Attach=;
Attach=;
Attach=EventID;
Attach();
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 ;
public System.Data.SqlTypes.SqlString ;
public byte[] ;
public int AddAttach()
{
string InsertSql="INSERT INTO Attach) VALUES (@" +
")";
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("@", System.Data.SqlDbType.VarChar, 50));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@", System.Data.SqlDbType.Image));
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@", 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["@"].Value=this.;
myCommand.Parameters["@"].Value=this.;
myCommand.Parameters["@"].Value=this.;
// 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;
}
}

标签: