Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Had Searched Everywhere To Write Binary Files In C#

I Had Only Get How To Do It in console

but i want it to be from text box to binary
how can i do it

What I have tried:

I Had  Searched Everywhere To Write Binary Files In C#

I Had Only Get How To Do It in console

but i want it to be from text box to binary
how can i do it
Posted
Updated 29-Dec-20 4:08am

The BinaryReader and BinaryWriter classes are used for reading from and writing to a binary file.

You can read details here: Reading from and Writing into Binary files - Tutorialspoint[^]

Example:
C#
using System;
using System.IO;

namespace BinaryFileApplication {
   class Program {
      static void Main(string[] args) {
         BinaryWriter bw;
         BinaryReader br;
         
         int i = 25;
         double d = 3.14157;
         bool b = true;
         string s = "I am happy"; //In your case you can get this string from the textbox.
         
         //create the file
         try {
            bw = new BinaryWriter(new FileStream("mydata", FileMode.Create));
         } catch (IOException e) {
            Console.WriteLine(e.Message + "\n Cannot create file.");
            return;
         }
         
         //writing into the file
         try {
            bw.Write(i);
            bw.Write(d);
            bw.Write(b);
            bw.Write(s);
         } catch (IOException e) {
            Console.WriteLine(e.Message + "\n Cannot write to file.");
            return;
         }
         bw.Close();
         
         //reading from the file
         try {
            br = new BinaryReader(new FileStream("mydata", FileMode.Open));
         } catch (IOException e) {
            Console.WriteLine(e.Message + "\n Cannot open file.");
            return;
         }
         
         try {
            i = br.ReadInt32();
            Console.WriteLine("Integer data: {0}", i);
            d = br.ReadDouble();
            Console.WriteLine("Double data: {0}", d);
            b = br.ReadBoolean();
            Console.WriteLine("Boolean data: {0}", b);
            s = br.ReadString();
            Console.WriteLine("String data: {0}", s);
         } catch (IOException e) {
            Console.WriteLine(e.Message + "\n Cannot read from file.");
            return;
         }
         br.Close();
         Console.ReadKey();
      }
   }
}

Output:
Integer data: 25
Double data: 3.14157
Boolean data: True
String data: I am happy

Try out.
 
Share this answer
 
v2
Comments
Vaishnav Ceh 29-Dec-20 10:01am    
Mate I Want It For Form Application
Sandeep Mewara 29-Dec-20 10:06am    
Yes. Put similar code like above on a form submit / button click - any event you want to use.

What's the issue with above in winforms?
Dave Kreskowiak 29-Dec-20 11:42am    
The type of applications has absolutely no impact on how the file is created/written/read. The code do so would be the same.
Or even easier than Sandeep's method:
public void WriteStringAsBinary(string path, string data)
    {
    byte[] bytes = Encoding.UTF8.GetBytes(data);
    File.WriteAllBytes(path, bytes);
    }
Though to be honest you will almost certainly get exactly the same result with this:
public void WriteStringAsBinary(string path, string data)
    {
    File.WriteAllText(path, data, Encoding.UTF8);
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900