Tweet
SIGN UP
MEMBER LOGIN:
TECHNOLOGIES
.NET 4.5
.NET Remoting in C#
Active Directory C#
ADO.NET in C#
AJAX in C#
Algorithms in C#
Android Programming
Articles C#
ASP, JavaScript, CSS
ASP.NET Controls in C#
ASP.NET MVC with C#
ASP.NET Programming
BizTalk Server
C# Assemblies
C# Language
C# Tutorials
C, C++, MFC
Career Advice
Chapters
Cloud Computing
COBOL.NET
Coding Best Practices
COM Interop
Compact Framework
Cryptography C#
Crystal Reports C#
Current Affairs
Custom Controls C#
Databases & DBA
Deployment
Design & Architecture
DirectX C#
Enterprise Development
Error Zone
Exception Handling C#
Expression Studio
F#
Files, Directories in C#
Financial Applications
Games Programming C#
GDI+ & Graphics
Hardware
How do I
HTML 5
Internet & Web
iPhone/iPad
Java
Java and .NET
JQuery
JSP
Leadership
Learn .NET
LINQ with C#
Metro Style Apps in C#
Mobile & Embedded
MonoDevelop
MSMQ in C#
Multithreading in C#
Networking
Office Development
OOP/OOD
Operating Systems
PHP
Printing in C#
Products
Project Management
Reports using C#
Robotics & Hardware
Security in .NET
SharePoint
Silverlight with C#
Smart Devices
Speech in C#
SQL
SQL Server 2012
String in C#
Team Foundation & VSS
Testing
Visual Basic .NET
Visual C#
Visual Studio .NET
Visual Studio 11
Visual Studio 2010
VS LightSwitch 2011
WCF with C#
Web Forms C#
Web Services in C#
WebForms Controls
Windows 8 in C#
Windows Controls C#
Windows Forms C#
Windows Phone in C#
Windows PowerShell
Windows Services in C#
Workflow Foundation in C#
WPF with C#
XAML with C#
XML in C#
XNA with C#
FORUMS
BLOGS
VIDEOS
INTERVIEWS
CERTIFICATIONS
DOWNLOADS
BOOKS
LINKS
NEWS
Learn .NET in 60 days – Part 1 (13 Labs)
Learn MVC (Model view controller) Step by Step ...
Learn C# Corner - Home
Using Border Radius and Gradients in CSS3: Part I
Learn C# Corner - Footer
Learn .NET and C# in 60 Days Lab13(Day 5): - C ...
iPhone 5 First Look
Samsung Galaxy Note Review
WCF - Authentication and Authorization in Ente ...
How to write a good article on C# Corner
Blog
File Encoding And Decoding In .Net Using Simple IO Operations
Posted by
Krishna Garad
in
Blogs
|
Files, Directories in C#
on
Aug 30, 2011
In this blog we will discuss how to encode and decode the files using simple IO operations in .Net.
Tweet
964
0
0
Download Files:
EncodeDecodeFile.zip
BackGround:
For providing security for for our files we need some encoding facility in application. Then we can send those encoded files over the network. Here we will see the encoding and decoding of files using System.IO namespace provided by .Net.
Let's start our work step-by-step
Step 1:
Create C# windows form application and design the form as bellow.
Step 2:
Add using directive to System.IO namespace of .net framework and write this two methods.
private
void
DecodeFile(
string
srcfile,
string
destfile)
{
string
src;
StreamReader
sr =
new
StreamReader
(srcfile)
src = sr.ReadToEnd();
sr.Close();
byte
[] bt64 = System.
Convert
.FromBase64String(src);
if
(
File
.Exists(destfile))
{
File
.Delete(destfile);
}
FileStream
sw =
new
FileStream
(destfile,
FileMode
.Create);
sw.Write(bt64, 0, bt64.Length);
sw.Close();
}
private
void
EncodeFile(
string
srcfile,
string
destfile)
{
string
dest;
FileStream
sr =
new
FileStream
(srcfile,
FileMode
.Open);
byte
[] srcbt =
new
byte
[sr.Length];
sr.Read(srcbt, 0, (
int
)sr.Length);
sr.Close();
dest = System.
Convert
.ToBase64String(srcbt);
StreamWriter
sw =
new
StreamWriter
(destfile,
false
);
sw.Write(dest);
sw.Close();
}
Decode and EncodeFile which will take sourcefilename and destination filenames as input. But here DecodeFile method source filename must be encoded file by EncodeFile.
Step 3:
Call this method in Encode and Decode button click event respectively. After encoding file you may see the file content will look like bellow screen shot.
While actual data of the file is something different like bellow.
Step 4:
Execute the application and do encoding and decoding of files from .net. It's pretty easy.
Conclusion:
Using normal System.IO operation we can encrypt file i.e. not readable format and again decrypt those file to original state.
share this blog :
Practical Approach of Getting Sp..
How to convert your file into binary form?
Related Blogs
How to convert your file into binary form?
Synchronizing Directories In Code
How to write or create a text file in C#
post comment
Sponsored by
Become a Sponsor
More Blogs from this Blogger
Manipulating Huge Integers In C#
File Encoding And Decoding In .Net Using Simple IO Operations
Call Stored Procedure With In/Out Parameter In 3-Tier
Calling DataSet with Stored Procedure in 3-tier
View All
Latest Blogs
Free Ride is Over for Desktop Developers in Visual Studio 11
DotNet developers most used application/tools launching through 'Run'
80-inch Windows 8 Tablet
Data encapsulation
Option to access the Column Names in Data table
Open multiple windows in browser startup. (Multiple homepages option)
I'm Sorry
const and readonly
Address, Binding and Contract in WCF
JQuery Maxchars Plugin
View All
Sponsored by
Become a Sponsor