943,832 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 4240
  • C# RSS
Jun 3rd, 2009
0

encode-decode to base64 string

Expand Post »
this is not a question, this is snippet to encode any file to base64 string and decode that encoded string to that file back. i attach the project to this post too.

Form1.cs :
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9.  
  10. namespace encode_decode_Base64
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void btnEncode_Click(object sender, EventArgs e)
  19. {
  20. if (!string.IsNullOrEmpty(txtInFile.Text))
  21. {
  22. FileStream fs = new FileStream(txtInFile.Text,
  23. FileMode.Open,
  24. FileAccess.Read);
  25. byte[] filebytes = new byte[fs.Length];
  26. fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
  27. string encodedData =
  28. Convert.ToBase64String(filebytes,
  29. Base64FormattingOptions.InsertLineBreaks);
  30. txtEncoded.Text = encodedData;
  31. }
  32.  
  33. }
  34.  
  35. private void btnDecode_Click(object sender, EventArgs e)
  36. {
  37. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  38. {
  39.  
  40. byte[] filebytes = Convert.FromBase64String(txtEncoded.Text);
  41. FileStream fs = new FileStream(saveFileDialog1.FileName,
  42. FileMode.CreateNew,
  43. FileAccess.Write,
  44. FileShare.None);
  45. fs.Write(filebytes, 0, filebytes.Length);
  46. fs.Close();
  47.  
  48. }
  49.  
  50. }
  51.  
  52. private void button1_Click(object sender, EventArgs e)
  53. {
  54. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  55. {
  56. txtInFile.Text = openFileDialog1.FileName;
  57. }
  58. }
  59.  
  60.  
  61. }
  62. }

Form1.Designer.cs:
C# Syntax (Toggle Plain Text)
  1. namespace encode_decode_Base64
  2. {
  3. partial class Form1
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows Form Designer generated code
  24.  
  25. /// <summary>
  26. /// Required method for Designer support - do not modify
  27. /// the contents of this method with the code editor.
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.btnEncode = new System.Windows.Forms.Button();
  32. this.btnDecode = new System.Windows.Forms.Button();
  33. this.txtEncoded = new System.Windows.Forms.TextBox();
  34. this.txtInFile = new System.Windows.Forms.TextBox();
  35. this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
  36. this.button1 = new System.Windows.Forms.Button();
  37. this.lblBrowse = new System.Windows.Forms.Label();
  38. this.lblEncoded = new System.Windows.Forms.Label();
  39. this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
  40. this.SuspendLayout();
  41. //
  42. // btnEncode
  43. //
  44. this.btnEncode.Location = new System.Drawing.Point(288, 87);
  45. this.btnEncode.Name = "btnEncode";
  46. this.btnEncode.Size = new System.Drawing.Size(75, 23);
  47. this.btnEncode.TabIndex = 1;
  48. this.btnEncode.Text = "Encode";
  49. this.btnEncode.UseVisualStyleBackColor = true;
  50. this.btnEncode.Click += new System.EventHandler(this.btnEncode_Click);
  51. //
  52. // btnDecode
  53. //
  54. this.btnDecode.Location = new System.Drawing.Point(288, 415);
  55. this.btnDecode.Name = "btnDecode";
  56. this.btnDecode.Size = new System.Drawing.Size(75, 23);
  57. this.btnDecode.TabIndex = 2;
  58. this.btnDecode.Text = "Decode";
  59. this.btnDecode.UseVisualStyleBackColor = true;
  60. this.btnDecode.Click += new System.EventHandler(this.btnDecode_Click);
  61. //
  62. // txtEncoded
  63. //
  64. this.txtEncoded.Location = new System.Drawing.Point(28, 155);
  65. this.txtEncoded.Multiline = true;
  66. this.txtEncoded.Name = "txtEncoded";
  67. this.txtEncoded.Size = new System.Drawing.Size(381, 228);
  68. this.txtEncoded.TabIndex = 3;
  69. //
  70. // txtInFile
  71. //
  72. this.txtInFile.Location = new System.Drawing.Point(28, 87);
  73. this.txtInFile.Name = "txtInFile";
  74. this.txtInFile.Size = new System.Drawing.Size(161, 20);
  75. this.txtInFile.TabIndex = 5;
  76. //
  77. // openFileDialog1
  78. //
  79. this.openFileDialog1.FileName = "openFileDialog1";
  80. //
  81. // button1
  82. //
  83. this.button1.Location = new System.Drawing.Point(212, 85);
  84. this.button1.Name = "button1";
  85. this.button1.Size = new System.Drawing.Size(43, 23);
  86. this.button1.TabIndex = 6;
  87. this.button1.Text = "...";
  88. this.button1.UseVisualStyleBackColor = true;
  89. this.button1.Click += new System.EventHandler(this.button1_Click);
  90. //
  91. // lblBrowse
  92. //
  93. this.lblBrowse.AutoSize = true;
  94. this.lblBrowse.Location = new System.Drawing.Point(35, 57);
  95. this.lblBrowse.Name = "lblBrowse";
  96. this.lblBrowse.Size = new System.Drawing.Size(113, 13);
  97. this.lblBrowse.TabIndex = 7;
  98. this.lblBrowse.Text = "Select a file to encode";
  99. //
  100. // lblEncoded
  101. //
  102. this.lblEncoded.AutoSize = true;
  103. this.lblEncoded.Location = new System.Drawing.Point(35, 126);
  104. this.lblEncoded.Name = "lblEncoded";
  105. this.lblEncoded.Size = new System.Drawing.Size(105, 13);
  106. this.lblEncoded.TabIndex = 8;
  107. this.lblEncoded.Text = "Encoded file content";
  108. //
  109. // Form1
  110. //
  111. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  112. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  113. this.ClientSize = new System.Drawing.Size(517, 684);
  114. this.Controls.Add(this.lblEncoded);
  115. this.Controls.Add(this.lblBrowse);
  116. this.Controls.Add(this.button1);
  117. this.Controls.Add(this.txtInFile);
  118. this.Controls.Add(this.txtEncoded);
  119. this.Controls.Add(this.btnDecode);
  120. this.Controls.Add(this.btnEncode);
  121. this.Name = "Form1";
  122. this.Text = "Form1";
  123. this.ResumeLayout(false);
  124. this.PerformLayout();
  125.  
  126. }
  127.  
  128. #endregion
  129.  
  130. private System.Windows.Forms.Button btnEncode;
  131. private System.Windows.Forms.Button btnDecode;
  132. private System.Windows.Forms.TextBox txtEncoded;
  133. private System.Windows.Forms.TextBox txtInFile;
  134. private System.Windows.Forms.OpenFileDialog openFileDialog1;
  135. private System.Windows.Forms.Button button1;
  136. private System.Windows.Forms.Label lblBrowse;
  137. private System.Windows.Forms.Label lblEncoded;
  138. private System.Windows.Forms.SaveFileDialog saveFileDialog1;
  139. }
  140. }
Attached Files
File Type: zip encode-decode-Base64.zip (36.5 KB, 167 views)
Similar Threads
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Jun 4th, 2009
0

Re: encode-decode to base64 string

Thanks for sharing. Use the code snippits area of DaniWeb to post your code snipits - http://www.daniweb.com/code/csharp.html
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
Jun 4th, 2009
0

Re: encode-decode to base64 string

Click to Expand / Collapse  Quote originally posted by Tekmaven ...
Thanks for sharing. Use the code snippits area of DaniWeb to post your code snipits - http://www.daniweb.com/code/csharp.html
it does not have attachment facility.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Adding rows to a datagrid.
Next Thread in C# Forum Timeline: wierd conclusion about foreach statement





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC