C#基础学习07(熟悉Winform、消息弹窗、IO文件操作)

一、消息弹窗

 1.窗体跳转

OrderForms of = new OrderForms();//new出新的窗体对象
this.Hide();        //隐藏当前窗体
of.ShowDialog();    //打开窗体
of.Show();          //打开隐藏的窗体

二、IO文件操作

例:通过路径读取活写入文本文件,显示到窗体中,复制按钮为复制一个视频案例。

private void btnWrite_Click(object sender, EventArgs e)//写入
{
    string path = tbPath.Text.Trim();
    FileStream fs = new FileStream(path,FileMode.Create,FileAccess.Write);
    StreamWriter sw = new StreamWriter(fs);
    sw.Write(tbDate.Text);
    sw.Close();
    fs.Close();
    tbDate.Text = "";
    MessageBox.Show("写入成功");
}

private void btnRead_Click(object sender, EventArgs e)//读取
{
    string path = tbPath.Text.Trim();
    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader(fs);
    tbDate.Text=sr.ReadToEnd();
    sr.Close();
    fs.Close();
}

private void btnCopy_Click(object sender, EventArgs e)//复制
{
    string path1 = "G:\\南山景区3D导游员.mp4";  //原始路径
    string path2 = @"G:\大片\成果.mp4";        //复制后的路径
    using (FileStream fReader = new FileStream(path1, FileMode.Open, FileAccess.Read))//只在using范围内有效
    {
        using (FileStream fWrite = new FileStream(path2, FileMode.Create, FileAccess.Write))
        {
            byte[] date = new byte[1024*1024*10];//10M
            int r = fReader.Read(date,0,date.Length);//r为每次读多少
            while (r>0)//通过r大小判断是否读取完成
            {
                fWrite.Write(date,0,r);//写入文件
                r = fReader.Read(date,0,date.Length);//继续读取,读完为止
            }
            MessageBox.Show("复制完成,快去欣赏吧!");
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值