C# 將檔案儲存為二進位

來源:互聯網
上載者:User

OleDbConnection Mycon = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=MyDb.mdb");<br /> FileStream fs = new FileStream(filename,FileMode.Open, FileAccess.Read);<br /> BinaryReader br = new BinaryReader(fs);<br /> byte[] mywave = br.ReadBytes((int)fs.Length);<br /> br.Close();<br /> fs.Close();</p><p> Mycon.Open();<br /> string sql = "insert into MyDataBase(wav,MyWord) VALUES (@wav,'"+textBox1.Text.Trim()+"')";<br /> OleDbCommand cmd = new OleDbCommand(sql, Mycon);<br /> cmd.Parameters.Add("@wav", OleDbType.Binary, photo.Length).Value = mywave ;<br /> cmd.ExecuteNonQuery();<br /> Mycon.Close();</p><p>#region 對文字檔的讀寫<br />FileStream fs = new FileStream(@"r:''txt.txt", System.IO.FileMode.OpenOrCreate,<br />System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);<br />FileStream f = File.Open(@"r:''f.txt", FileMode.OpenOrCreate,<br />FileAccess.ReadWrite, FileShare.ReadWrite);<br />string s = "秋偉光lov.09823p''''=!@#%.;";<br />byte[]inb = System.Text.Encoding.UTF8.GetBytes(s); //和下面一樣這個容易理解<br />//byte[] inb = new UTF8Encoding(true).GetBytes(s);//和上面的一樣效果<br />fs.Write(inb, 0, inb.Length);<br />fs.Flush(); //清空緩衝<br />fs.Close();<br />FileStream ff = File.OpenRead(@"r:''txt.txt");<br />byte[]a = new byte[ff.Length];<br />ff.Read(a, 0, (int)a.Length);<br />f.Write(a, 0, (int)ff.Length);<br />ff.Close();<br />f.Close();<br />#endregion</p><p>#region 對二進位檔案的讀寫<br />//System.IO.BinaryReader: 讀二進位檔案<br />//System.IO.BinaryWriter: 寫二進位檔案<br />//FileStream fs1 = new FileStream(@"r:''gif.gif",FileMode.OpenOrCreate,FileAccess.ReadWrite);<br />//FileStream fs2 = new FileStream(@"r:''p1.bmp",FileMode.OpenOrCreate,FileAccess.ReadWrite);<br />//FileStream fw = new FileStream(@"r:''new.txt",FileMode.OpenOrCreate,FileAccess.ReadWrite);<br />//BinaryReader br = new BinaryReader(fs1);<br />//BinaryWriter bw = new BinaryWriter(fw);<br />//bw.Write(br.ReadInt32());<br />#endregion

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.