file+binari

=**File binari**=

 Vediamo come è possibile **copiare il contenuto di un file qualunque** in un altro. Poiché il file di origine può essere qualsiasi, lo si tratta come un flusso generico di byte. Il modo più semplice consiste nell'usare il metodo File.Copy(fileSorgente, fileDestinazione) . Ecco come: using System; using System.IO;

namespace FileBinariCopiaDaA {  class Program  {  static void Main(string[] args)  {  string nomeFileOrigine = "berly.png"; <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> string nomeFileDest = "mostro.png"; <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> **File.Copy(nomeFileOrigine, nomeFileDest);** <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> } <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> } <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;">} <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 14.6667px; text-align: justify;">Facile no?

<span style="color: #000000; display: block; font-family: Courier New,monospace; font-size: 12px; text-align: justify;"><span style="font-family: Verdana,sans-serif; font-size: 14.6667px;">Ora vediamo di scrivere noi il metodo <span style="font-family: Courier New,monospace; font-size: 14.6667px;">Copy <span style="font-family: Verdana,sans-serif; font-size: 14.6667px;">. Perchè mai? Vi chiederete, per puro sadismo,ovviamente ;) <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 14.6667px; text-align: justify;">Ecco il codice: <span style="color: #000000; display: block; font-family: Courier New,monospace; font-size: 12px; text-align: justify;">using System; <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;">using System.IO;

<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;">namespace FileBinariCopiaDaA <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;">{ <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> class Program <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> { <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> static void Main(string[] args) <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> { <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> string nomeFileOrigine = "berly.png"; <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> string nomeFileDest = "mostro.png"; <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> CopiaFile(nomeFileOrigine, nomeFileDest2); <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> }

<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> **public static void CopiaFile(string nomeFileOrigine, string nomeFileDest)** {

<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> BinaryReader br; <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> BinaryWriter bw;

<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> br = new BinaryReader(File.Open(nomeFileOrigine, FileMode.Open)); <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> bw = new BinaryWriter(File.Open(nomeFileDest, FileMode.Create));

<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> **byte[] buffer = new byte[32768];** <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> int letti;

<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> **while ((letti = br.Read(buffer, 0, buffer.Length)) > 0)** { <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> **bw.Write(buffer, 0, letti);** <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> } <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> br.Close; <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> bw.Close; <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> } <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> } <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;">}

<span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 12px; text-align: center;">**Alla sorgente: da string a byte ; alla destinazione: da byte a string** <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 12px; text-align: justify;"> Quando si ha a che fare con una rete (Internet ad esempio) : <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 14.6667px; text-align: justify;">Spesso in rete si devono inviare stringhe e ricevere stringhe. <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 14.6667px; text-align: justify;">Vediamo come simulare questa trasmissione: un file rappresenterà la rete. <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 14.6667px; text-align: justify;">L'applicazione che scriveremo farà dunque le seguenti attività: <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 14.6667px; text-align: justify;">Ecco il codice: <span style="color: #000000; display: block; font-family: Courier New,monospace; font-size: 12px; text-align: justify;">using System; <span style="color: #000000; display: block; font-family: Courier New,monospace; font-size: 12px; text-align: justify;">**using System.Text; //per Encoding//** //<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;">using System.IO; //per FileStream
 * <span style="font-family: Verdana,sans-serif; font-size: 12px;">i dati vengono **inviati** come sequenze di **byte**
 * <span style="font-family: Verdana,sans-serif; font-size: 12px;">i byte **ricevuti** vengono **convertiti nel tipo atteso**
 * 1) <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 14.6667px; text-align: justify;">Simulazione Invio:
 * 2) <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 12px; text-align: justify;">converte una stringa in un vettore di byte
 * 3) <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 12px; text-align: justify;">scrive i byte nel file
 * 4) <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 14.6667px; text-align: justify;">Simulazione Ricezione:
 * 5) <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 12px; text-align: justify;">legge dal file un blocco di byte
 * 6) <span style="color: #000000; display: block; font-family: Verdana,sans-serif; font-size: 12px; text-align: justify;">converte il blocco di byte in una stringa

<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;">namespace FileDaByteAChar <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;">{ <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> class Program <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> { <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> static void Main(string[] args) <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> { <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> //otteniamo un FileStream// //<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> using (FileStream fs = File.Open("dati.dat", FileMode.Create)) // //<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> { // codifica: **da stringa a byte**<span style="color: #000000; font-family: Consolas,sans-serif; font-size: 12px;">**[]** <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> string str = "ciao"; <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> **byte[] strInByte = Encoding.Default.GetBytes(str);** <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> //**scrivo byte** nel file per simulare l'**INVIO**// //<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> fs.Write(strInByte, 0, strInByte.Length); //

reset posizione interna al file<span style="color: #000000; font-family: Consolas,sans-serif; font-size: 12px;"> per potervi leggere <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> fs.Position = 0;

<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> //**leggo byte** dal file:// uno alla volta perchè non posso sapere quanti sono. <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> //si simula così la **RICEZIONE**// //<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> byte[] byteDalFile = new byte[strInByte.Length]; // //<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> for (int i = 0; i < strInByte.Length; i++) // //<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> { // //<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> byteDalFile[i] = (byte)fs.ReadByte; // //<span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> } // decodifico **da byte[] a stringa** <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> Console.WriteLine(**Encoding.Default.GetString(byteDalFile)**); <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> } //fine using <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> Console.ReadKey; <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> } <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;"> } <span style="color: #000000; font-family: Courier New,monospace; font-size: 12px;">}