C#分塊拷貝大檔案

來源:互聯網
上載者:User

標籤:style   blog   color   檔案   資料   os   

  //定義源檔案和目標檔案,絕對路徑   public static string source = @"E:\C#\C#程式設計語言詳解.pdf";    //2014-6-10 Training            //拷貝大檔案,分塊拷貝            AddBigFile(source);    /// <summary>        /// 拷貝大檔案        /// </summary>        /// <param name="source">原絕對路徑</param>        private static void AddBigFile(string source)        {            int i = 0;            using (FileStream fsRead = new FileStream(source, FileMode.Open))            {                byte[] byts = new byte[1024 * 1024 * 10];                while (true)                {                    int r = fsRead.Read(byts, 0, byts.Length);                    if (r <= 0)                    {                        Console.WriteLine("----End----");                        break;                    }                    DataBaseController.AddFiles(source, i, byts);                    Console.WriteLine("FileName:" + source + "第 " + i + "個" + "大小:" + byts.Length);                    i++;                }            }        }DataBaseController.AddFiles函數是拿到位元據,插入資料庫操作: public static class DataBaseController    {       public static readonly string connstr = "Data Source=.;Initial Catalog=AddFile;Persist Security Info=True;User ID=sa;Password=sa";       public static string Tosource = @"E:\C#\Copy123C#程式設計語言詳解.pdf";       public static void AddFiles(string FileName,int Code,byte[] Data)       {           string sql = @"insert into FileBlock(FileName, Code, Data) values(@FileName, @Code, @Data)";           DateTime time = DateTime.Now;           SqlParameter[] pars = {                                new SqlParameter("@FileName",FileName),                                new SqlParameter("@Code",Code),                                new SqlParameter("@Data",Data)                                                               };           int count = SqlHelper.ExecuteNonQuery(connstr, CommandType.Text, sql, pars);       }       public static void GetFilesByName(string FileName)       {           string sql = @"select * from FileBlock where FileName = @FileName";           DateTime time = DateTime.Now;           SqlParameter[] pars = {                                new SqlParameter("@FileName",FileName),                                                    };           List<FileBlock> list = new List<FileBlock>();           using (SqlDataReader reader = SqlHelper.ExecuteReader(connstr, CommandType.Text, sql, pars))           {               list = Application.Data2Objects<FileBlock>(reader);           }           //迴圈查出檔案的多個塊,拼接寫在一個檔案中           FileStream fs = new FileStream(Tosource,FileMode.Create);           foreach (var item in list)           {               fs.Write(item.Data,0,item.Data.Length);           }           //byte[] data = new byte[fs.Length];           //fs.Read(data,0,fs.Length);           //fs.Seek(0,SeekOrigin.Begin);          // return data;       }    }   public class FileBlock   {       public int ID { get; set; }       public string  FileName { get; set; }       public int Code { get; set; }       public byte[] Data { get; set; }   }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.