Saving and reading data in Binary Format

fatih uyanık 100 Reputation points
2024-07-08T11:21:44.9866667+00:00

Hello

I am developing a Firebase supported project. What I want to do is to take the current data and keep it in a file in Binary format so that it cannot be read easily. I thought of such a method because the data does not contain very sensitive data. I think I should use BinaryWriter and BinaryReader classes for this. However, before I put it into code, I would like to get your opinion. How can I go about this? What would be your suggestions for me?

Thanks.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,007 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 46,456 Reputation points Microsoft Vendor
    2024-07-09T02:54:51.8766667+00:00

    Hi @fatih uyanık , Welcome to Microsoft Q&A,

    First of all, Q&A does not provide support for third-party products such as Firebase. It is assumed that you already have code for Firebase data.

    1. Use BinaryWriter to write data to a file.
    2. Use BinaryReader to read data from a file.
    using System;
    using System.IO;
    using System.Threading.Tasks;
    using Firebase.Database;
    using Firebase.Database.Query;
    
    public class FirebaseBinaryExample
    {
        private static readonly string FirebaseUrl = "https://your-firebase-url.firebaseio.com/";
        private static readonly string FirebaseSecret = "your-firebase-secret";
    
        public static async Task Main(string[] args)
        {
            var firebaseClient = new FirebaseClient(
                FirebaseUrl,
                new FirebaseOptions
                {
                    AuthTokenAsyncFactory = () => Task.FromResult(FirebaseSecret)
                });
    
            //retrieve data
            var data = await firebaseClient
                .Child("path/to/your/data")
                .OnceAsync<MyDataType>();
    
            // Writing binary files
            using (FileStream fs = new FileStream("data.bin", FileMode.Create))
            using (BinaryWriter writer = new BinaryWriter(fs))
            {
                foreach (var item in data)
                {
                    // Assume MyDataType has properties Id and Name
                    writer.Write(item.Object.Id);
                    writer.Write(item.Object.Name);
                }
            }
    
            //Reading binary files
            using (FileStream fs = new FileStream("data.bin", FileMode.Open))
            using (BinaryReader reader = new BinaryReader(fs))
            {
                while (fs.Position < fs.Length)
                {
                    int id = reader.ReadInt32();
                    string name = reader.ReadString();
                    Console.WriteLine($"Id: {id}, Name: {name}");
                }
            }
        }
    
        public class MyDataType
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.