C#文件操作的簡略實例。本站提示廣大學習愛好者:(C#文件操作的簡略實例)文章只能為提供參考,不一定能成為您想要的結果。以下是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();
文件的復制
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");