C#文件操作的簡單實例
更新時間:2014年02月15日 16:35:33 作者:
這篇文章主要介紹了C#文件操作的簡單實例,需要的朋友可以參考下
文件的讀取
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#中Predicate<T>與Func<T, bool>泛型委托的用法實例
這篇文章主要介紹了C#中Predicate<T>與Func<T, bool>泛型委托的用法,指出了其用法中的誤區(qū)及易錯點,有助于更好的理解泛型委托的用法,需要的朋友可以參考下2014-09-09C#中用foreach語句遍歷數(shù)組及將數(shù)組作為參數(shù)的用法
這篇文章主要介紹了C#中用foreach語句遍歷數(shù)組及將數(shù)組作為參數(shù)的用法,C#的數(shù)組可作為實參傳遞給方法形參,需要的朋友可以參考下2016-01-01結(jié)合Visual C#開發(fā)環(huán)境講解C#中事件的訂閱和取消訂閱
這篇文章主要介紹了C#中事件的訂閱和取消訂閱,結(jié)合Visual C#開發(fā)環(huán)境來進行講解,Visual C#被集成在微軟的IDE程序Visual Studio中,需要的朋友可以參考下2016-01-01C#調(diào)用百度翻譯API實現(xiàn)一個翻譯功能
一直喜歡用Google Translate API進行在線翻譯,但是服務(wù)越來越慢這篇文章,所以只能換一個了,主要給大家介紹了關(guān)于C#調(diào)用百度翻譯API實現(xiàn)一個翻譯功能的相關(guān)資料,需要的朋友可以參考下2021-06-06C#中DataSet、DataTable、DataRow數(shù)據(jù)的復(fù)制方法
這篇文章介紹了C#中DataSet、DataTable、DataRow數(shù)據(jù)的復(fù)制方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07C#?OpenCvSharp?顏色反轉(zhuǎn)實例詳解
OpenCVSharp是OpenCV的.NET?wrapper,它比Emgucv更接近于原始的OpenCV,并且有很多的樣例參考,其采用LGPL發(fā)行,對商業(yè)應(yīng)用友好(基本上相當于BSD),這篇文章主要介紹了C#?OpenCvSharp?顏色反轉(zhuǎn)的知識,需要的朋友可以參考下2024-02-02