Created
November 17, 2019 22:44
-
-
Save Ushiosan23/74cbb10e3362d6704e009bfd5faa788a to your computer and use it in GitHub Desktop.
Write file with binary data. C#.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace ConsoleApp1.Company { | |
| /// <summary> | |
| /// Person. | |
| /// </summary> | |
| [Serializable] | |
| public class Person { | |
| #region Private properties | |
| /// <summary> | |
| /// The name. | |
| /// </summary> | |
| private string _name; | |
| /// <summary> | |
| /// The born. | |
| /// </summary> | |
| private DateTime _born; | |
| #endregion | |
| #region Public properties | |
| /// <summary> | |
| /// Gets the name. | |
| /// </summary> | |
| /// <value>The name.</value> | |
| public string Name { get { return _name; } } | |
| /// <summary> | |
| /// Gets the born. | |
| /// </summary> | |
| /// <value>The born.</value> | |
| public DateTime Born { get { return _born; } } | |
| #endregion | |
| /// <summary> | |
| /// Initializes a new instance of the <see cref="ConsoleApp1.Company.Person"/> class. | |
| /// </summary> | |
| /// <param name="name">Name.</param> | |
| /// <param name="born">Born.</param> | |
| public Person (string name, DateTime born) { | |
| _name = name; | |
| _born = born; | |
| } | |
| /// <summary> | |
| /// Initializes a new instance of the <see cref="ConsoleApp1.Render.Person"/> class. | |
| /// </summary> | |
| /// <param name="name">Name.</param> | |
| public Person (string name) : this(name, DateTime.Now) { | |
| } | |
| /// <summary> | |
| /// Returns a string that represents the current object. | |
| /// </summary> | |
| /// <returns>A string that represents the current object.</returns> | |
| public override string ToString() { | |
| return string.Format("[Name: {0}, Born: {1}]", _name, _born.ToString()); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.Runtime.Serialization.Formatters.Binary; | |
| using ConsoleApp1.Company; | |
| namespace ConsoleApp1 { | |
| class Program { | |
| public static void Main(string[] args) { | |
| Person person = new Person("Brian Eduardo"); | |
| string file = "file.dat"; | |
| FileStream fStream = new FileStream(file, FileMode.Create); | |
| BinaryWriter fWriter = new BinaryWriter(fStream); | |
| BinaryFormatter formatter = new BinaryFormatter(); | |
| MemoryStream mStream = new MemoryStream(); | |
| formatter.Serialize(mStream, person); | |
| fWriter.Write(mStream.ToArray()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment