关闭

c# 文件读与写

61人阅读 评论(0) 收藏 举报
分类:

将信息写入文件:

                        SaveFileDialog sfd = new SaveFileDialog();
                        sfd.Title = "请选择要保存的文件";
                        sfd.InitialDirectory = @"D:\【仓库】\OVER包\Socket测试";
                        sfd.Filter = "视频文件(*.avi)|*.avi|所有文件(*.*)|*.*";
                        //sfd.ShowDialog(this);//WIN7.WIN8下会弹不出来,此时须多加一个this.
                        sfd.ShowDialog();
                        string path = sfd.FileName;
                        using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                        {
                            fsWrite.Write(buffer,1,r-1);
                        }
                        MessageBox.Show("文件保存完毕!");


从文件中读出信息:

                //获取要发送文件的路径
            string path = this.textBox3.Text;
            using (FileStream fsRead= new FileStream(path,FileMode.Open,FileAccess.Read))
            {
                byte[] buffer = new byte[1024 * 1024 * 5];
                int r = fsRead.Read(buffer, 0, buffer.Length);

                List<byte> list = new List<byte>();
                list.Add(1);
                list.AddRange(buffer);
                byte[] newBuffer = list.ToArray();
                

                //获取用户在下拉框中选取中的IP地址
                string ip = this.comboBox1.SelectedItem.ToString();
                dicsocket[ip].Send(newBuffer, 0, r+1, SocketFlags.None);
            }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10607次
    • 积分:258
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:8篇
    • 译文:0篇
    • 评论:0条
    文章分类