C#文件操作的簡單實(shí)例
更新時間:2014年02月15日 16:35:33 作者:
這篇文章主要介紹了C#文件操作的簡單實(shí)例,需要的朋友可以參考下
文件的讀取
FileStream fs = new FileStream(@"D:\12.txt", FileMode.Open);
byte[] buffer = new byte[1024 * 1024];
fs.Read(buffer, 0, buffer.Length);
string content = Encoding.Default.GetString(buffer);
textBox1.Text = content;
fs.Dispose();
文件的保存
SaveFileDialog sfd = new SaveFileDialog();
DialogResult rst = sfd.ShowDialog();
if(rst==System.Windows.Forms.DialogResult.OK)
{
FileStream fs = new FileStream(sfd.FileName,FileMode.Create);
string content = textBox1.Text;
byte[] buffer = ASCIIEncoding.UTF8.GetBytes(content);
fs.Write(buffer,0,buffer.Length);
fs.Dispose();
文件的復(fù)制
FileStream streamread = new FileStream(@"D:\123.wmv",FileMode.Open);
FileStream streamwrite = new FileStream(@"F:\1212.wmv",FileMode.Create);
byte[]buffer=new byte[1024*1024*3];
int Length;
do
{
Length = streamread.Read(buffer,0, buffer.Length);
streamwrite.Write(buffer,0, Length);
}
while (Length == buffer.Length);
streamread.Dispose();
streamwrite.Dispose();
MessageBox.Show("Copy Success");
復(fù)制代碼 代碼如下:
FileStream fs = new FileStream(@"D:\12.txt", FileMode.Open);
byte[] buffer = new byte[1024 * 1024];
fs.Read(buffer, 0, buffer.Length);
string content = Encoding.Default.GetString(buffer);
textBox1.Text = content;
fs.Dispose();
文件的保存
復(fù)制代碼 代碼如下:
SaveFileDialog sfd = new SaveFileDialog();
DialogResult rst = sfd.ShowDialog();
if(rst==System.Windows.Forms.DialogResult.OK)
{
FileStream fs = new FileStream(sfd.FileName,FileMode.Create);
string content = textBox1.Text;
byte[] buffer = ASCIIEncoding.UTF8.GetBytes(content);
fs.Write(buffer,0,buffer.Length);
fs.Dispose();
文件的復(fù)制
復(fù)制代碼 代碼如下:
FileStream streamread = new FileStream(@"D:\123.wmv",FileMode.Open);
FileStream streamwrite = new FileStream(@"F:\1212.wmv",FileMode.Create);
byte[]buffer=new byte[1024*1024*3];
int Length;
do
{
Length = streamread.Read(buffer,0, buffer.Length);
streamwrite.Write(buffer,0, Length);
}
while (Length == buffer.Length);
streamread.Dispose();
streamwrite.Dispose();
MessageBox.Show("Copy Success");
相關(guān)文章
C#實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及contextMenu自定義
ContextMenu 類表示當(dāng)用戶在控件或窗體的特定區(qū)域上單擊鼠標(biāo)右鍵時會顯示的快捷菜單,要想實(shí)現(xiàn)自定義的Menu和ContextMenu效果,大家可以通過派生ProfessionalColorTable類,下面小編把實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及ContextMenu自定義給大家整理一下2015-08-08
C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫中的方法
這篇文章主要介紹了C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫中的方法,結(jié)合實(shí)例形式詳細(xì)分析了C#讀取Excel表數(shù)據(jù)及導(dǎo)入Sql Server數(shù)據(jù)庫的具體操作步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
C#如何實(shí)現(xiàn)監(jiān)控手機(jī)屏幕(附源碼下載)
這篇文章主要介紹了C#如何實(shí)現(xiàn)監(jiān)控手機(jī)屏幕(附源碼下載),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
C#開發(fā)WinForm清空DataGridView控件綁定的數(shù)據(jù)
本文詳細(xì)講解了C#開發(fā)WinForm清空DataGridView控件綁定數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
C#如何在窗體程序中操作數(shù)據(jù)庫數(shù)據(jù)
這篇文章主要介紹了C#如何在窗體程序中操作數(shù)據(jù)庫數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04

