Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Aprire file in modalità binaria
Forum - C# / VB.NET - Aprire file in modalità binaria

Avatar
Rex Romae (Normal User)
Newbie


Messaggi: 9
Iscritto: 30/12/2007

Segnala al moderatore
Postato alle 16:49
Sabato, 21/06/2008
Questo topic è stato chiuso dal moderatore

Ho cercato materiale su motori di ricerca ma non ho trovato quello che mi interessa. Devo aprire il file in modalità binaria e poi leggerlo come posso fare??

Codice sorgente - presumibilmente C# / VB.NET

  1. // Select the file
  2. OpenFileDialog file = new OpenFileDialog();
  3. file.Title = "Select a file";
  4. file.Filter = "All Files (*.*)|*.*";
  5. file.ShowDialog();
  6. textBox1.Text = file.FileName;


PM
Avatar
crash outside control (Normal User)
Expert


Messaggi: 217
Iscritto: 12/02/2008

Segnala al moderatore
Postato alle 18:05
Sabato, 21/06/2008
Prova con questo:

Codice sorgente - presumibilmente VB.NET

  1. Dim new_stream As Stream = File.Open("f:\a.bin", FileMode.Open, FileAccess.Read)
  2.         Using bw As New BinaryReader(new_stream)
  3.             Do Until bw.PeekChar() = -1
  4.                 txtleggi.Text &= bw.ReadByte
  5.             Loop
  6.         End Using



Poi ci fai sapere, ciao :)

PM
Avatar
Rex Romae (Normal User)
Newbie


Messaggi: 9
Iscritto: 30/12/2007

Segnala al moderatore
Postato alle 20:50
Sabato, 21/06/2008
Scusa ho scordato di specificare che sto programmando in C# ora e il codice mi serviva per C#... cmq l'ho tradotto e ora ho sistemato un po le cose ma non riesco a capire dove sbaglio xke l'applicazione si impalla quando leggo un semplice file.
Potete aiutarmi?
Codice sorgente - presumibilmente Delphi

  1. // Select the file
  2.             OpenFileDialog file = new OpenFileDialog();
  3.             file.Title = "Select a file";
  4.             file.Filter = "All Files (*.*)|*.*";
  5.             file.ShowDialog();
  6.             textBox1.Text = file.FileName;
  7.  
  8.             // Open and Read the file
  9.             Stream new_stream = File.Open(file.FileName, FileMode.Open, FileAccess.Read);
  10.             BinaryReader bw = new BinaryReader(new_stream);
  11.             while (bw.PeekChar() >= 0)
  12.             {
  13.                 textBox2.Text = bw.ToString();
  14.             }



grazie mille :k:

PM
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Segnala al moderatore
Postato alle 9:26
Domenica, 22/06/2008
Codice sorgente - presumibilmente Delphi

  1. // Select the file
  2. OpenFileDialog file = new OpenFileDialog();
  3. file.Title = "Select a file";
  4. file.Filter = "All Files (*.*)|*.*";
  5. file.ShowDialog();
  6. textBox1.Text = file.FileName;
  7.  
  8. // Open and Read the file
  9. Stream new_stream = File.Open(file.FileName, FileMode.Open, FileAccess.Read);
  10. BinaryReader bw = new BinaryReader(new_stream);
  11. while (bw.PeekChar() >= 0)
  12. {
  13.    textBox2.Text += bw.ReadChar();
  14. }
  15. bw.Close();


bw.ToString ti restituisce la stringa "System.IO.BinaryReader" e non legge niente, quindi non avanza di posizione. In conclusione PeekChar restituisce sempre il valore del primo byte, e sempre maggiore di -1.

Ultima modifica effettuata da Il Totem il 22/06/2008 alle 9:29
PM
Avatar
Rex Romae (Normal User)
Newbie


Messaggi: 9
Iscritto: 30/12/2007

Segnala al moderatore
Postato alle 13:45
Domenica, 22/06/2008
grazie ho risolto!:k:

PM