标签:des style class blog tar ext
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FileOperation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//选择文件框
private void btnOpenFile_Click(object sender, EventArgs e)
{
//new 选择文件框 对象
OpenFileDialog ofd = new OpenFileDialog();
//设置选择文本框的起始位置---桌面
ofd.InitialDirectory = @"C:\Users\lantian\Desktop";
//用户点击确认
if (ofd.ShowDialog() == DialogResult.OK)
{
//保存当前选择文件的路径,显示在文本框
txtOpenFilePath.Text = ofd.FileName;
}
}
/// <summary>
/// 保存文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSaveFile_Click(object sender, EventArgs e)
{
//获取多行文本的内容
string strContent = txtInPut.Text;
//实例化 FileStream---导入命名空间using System.IO;
//参数:文件路径,制定操作系统打开文件的方式Create----有则覆盖,无则创建
//凡是和网络,文件操作相关的,都应该(1)try...catch(2)销毁,用using
using (FileStream fs = new FileStream(txtOpenFilePath.Text,FileMode.Create))
{
//创建字符数组,并指定字符数组的大小1024*1024*4,相当于4M
//byte[] byteText=new byte[1024*1024*4];
//字符串转换成字节数组----Encoding.UTF8.GetBytes这个方法。
byte[] byteText = Encoding.UTF8.GetBytes(strContent);
//参数:要写入到文件的数据数组,从数组的第几个开始写,一共写多少个字节
fs.Write(byteText,0,byteText.Length);
MessageBox.Show("保存成功!");
}
}
/// <summary>
/// 读取文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReadFile_Click(object sender, EventArgs e)
{
//创建文件流(文件路径,文件操作方式.打开)
using (FileStream fs = new FileStream(txtOpenFilePath.Text, FileMode.Open))
{
//创建一个容量4M的数组
byte[] byteText = new byte[1024 * 1024 * 4];
//从文件中读取数据写入到数组中(数组对象,从第几个开始读,读多少个)
//返回读取的文件内容真实字节数
int length =fs.Read(byteText,0,byteText.Length);
//如果字节数大于0,则转码
if (length > 0)
{
//将数组转以UTF-8字符集编码格式的字符串
txtInPut.Text = Encoding.UTF8.GetString(byteText);
MessageBox.Show("读取成功");
}
}
}
}
}
标签:des style class blog tar ext
原文地址:http://www.cnblogs.com/skyl/p/3795280.html