• FileStream类操作文件


      private void buttonselect_Click (object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = "请选择要复制的文件";
                ofd.InitialDirectory = @"C:UsersSpringRainDesktop";
                ofd.Filter = "所有文件|*.*";
                ofd.ShowDialog();
                textBoxSelect.Text = ofd.FileName;
            }

            private void buttonsave_Click (object sender, EventArgs e)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title = "请选择要保存文件的路径";
                sfd.InitialDirectory = @"C:UsersSpringRainDesktop";
                sfd.Filter = "所有文件|*.*";
                sfd.ShowDialog();
                textBoxSave.Text = sfd.FileName;
                //先读取 再写入
                using (FileStream fsRead = new FileStream(textBoxSelect.Text.Trim(), FileMode.OpenOrCreate, FileAccess.Read))
                {
                    using (FileStream fsWrite = new FileStream(textBoxSave.Text.Trim(), FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        //设置进度条
                        progressBarFile.Maximum = (int)fsRead.Length;

                        byte[] buffer = new byte[1024 * 1024 * 3];
                        while (true)
                        {
                            int r = fsRead.Read(buffer, 0, buffer.Length);
                            if (r == 0)
                            {
                                break;
                            }
                         
                            fsWrite.Write(buffer, 0, r);
                            progressBarFile.Value = (int)fsWrite.Length;
                        }

                        MessageBox.Show("保存成功");

                    }
                }

      

  • 相关阅读:
    常用命令
    linux是文件里的内容整齐
    centos 7新机使用前操作
    Symmetric Tree @leetcode
    Binary Tree Inorder Traversal @leetcode
    [转]快速排序
    Remove Duplicates from Sorted Array @leetcode
    Remove Element @leetcode
    随笔1
    Unique Binary Search Trees @leetcode
  • 原文地址:https://www.cnblogs.com/netlws/p/8886929.html
Copyright © 2020-2023  润新知