惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - loveabel

汉鼎咨询:创业板上市企业募投项目变更原因深度分析 [导入]球员各个位置的关键属性问题 好好学习JAVA和.Net hibernate的语法怎么搞 一些关于中文乱码问题的一些解决方案和经验 前台对象的事件一览 复制表结构的通用存储过程 webconfig身份验证票 什么是web.config C#中使用DES HMACSHA1 SHA1 RC2 MD5 进行字符串加密的例程 两台SqlServer数据同步解决方案 正则表达式 自认为以下文章比较经典,希望对初学者有用^_^ IT人士群聚喝酒的讲究 谁在呐喊软件蓝领,那是他们准备开培训学校,谁在呐喊自主版权,那是他希望民族主义帮助销售,谁在呐喊“核心技术”,这多半是准备骗政府投资。 日历 系统数据库 sql server中扩展存储过程随笔(几个有用的PROCEDURE小总结) 修改数据库(设置数据库选项)
c#中对文件的操作小结
loveabel · 2005-04-18 · via 博客园 - loveabel

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;
    }
            

}