C#文件操作的简单实例
程序员文章站
2024-02-24 21:55:40
文件的读取复制代码 代码如下: filestream fs = new filestream(@"d:\12.txt", filemode.open); ...
文件的读取
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");
复制代码 代码如下:
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");