How to compress and decompress files with GZip in .NET C#

You have probably seen compressed files with the “gz” extension. These are files that hold a single compressed file according to the GZIP specifications.

GZip files are represented by the GZipStream object in .NET. It’s important to note that the GZip format doesn’t support adding multiple files to the same .gz file. If you need to insert multiple files in a GZip file then you’ll need to create a “tar” file first which bundles the individual files and then compresses the tar file itself. The result will be a “.tar.gz” file. At present tar files are not supported in .NET. They are supported by the ICSharpCode SharpZipLib library available here. We’ll look at tar files in another post soon.

With that in mind let’s see how a single file can be gzipped:

Read more of this post

Using isolated storage for application-specific data in C# .NET

There’s a special storage location for a .NET application on Windows that is allocated to that application. It’s called isolated storage and it’s an optimal place to store files by an application that doesn’t have full access to the file system. Writing to and reading from isolated storage doesn’t require any extra security check. An application without full access to the file system will be able to use its allocated slot in isolated storage and nothing else. It’s an ideal mechanism for storing e.g. application state.

However, don’t confuse isolated storage with file security. It is still a “normal” location on disk with a file path. A typical location is under users/[username]/appdata/local. Therefore you or full-trust applications can still find and modify the files saved in isolated storage. However, limited-trust applications won’t be able to access any other part of the file system.

Read more of this post

Creating temporary files on Windows using C# .NET

It’s trivial to create a temporary file using the Path class in .NET. Let’s first see how to find the path to the current user’s temp folder:

string tempFolderPath = Path.GetTempPath();

In my case it returns C:\Users\andras.nemes\AppData\Local\Temp\ which is the same as the value of the TEMP environment variable at the user level on my PC.

Read more of this post

Monitor the file system with FileSystemWatcher in C# .NET

In this post we’ll look at how you can use the FileSystemWatcher object to monitor the Windows file system for various changes.

A FileSystemWatcher object enables you to be notified when some change occurs in the selected part of the file system. This can be any directory, such as “c:\” or any subdirectory under the C: drive. So if you’d like to make sure you’re notified if a change occurs on e.g. “c:\myfolder” – especially if it’s editable by your colleagues – then FileSystemWatcher is a good candidate.

Consider the following Console application:

Read more of this post

Packing and unpacking files using Zip archives in .NET

We’ve looked at a couple of (de)compression techniques available in .NET in previous posts, see link below. Here we’ll look at how to compress multiple files into well-known ZIP files using .NET.

.NET4.5 has added native support for ZIP files, though you need to add the following library to reach the new functions:

New system compression dll

Say you’d like to compress all files within a folder:

Read more of this post

Compressing and decompressing files with BZip2 in .NET C#

BZip2 is yet another data compression algorithm, similar to GZip and Deflate. There’s no native support for BZip2 (de)compression in .NET but there’s a NuGet package provided by

Read more of this post

5 ways to write to a file with C# .NET

It’s a common scenario that you need to write some content to a file. Here you see 5 ways to achieve that.

1. Using a FileStream:

private void WriteToAFileWithFileStream()
	using (FileStream target = File.Create(@"c:\mydirectory\target.txt"))
		using (StreamWriter writer = new StreamWriter(target))
			writer.WriteLine("Hello world");

Read more of this post


A great site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Love to use Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Ricos Blog zu Softwaredesign- und architektur

Ideen und Gedanken rund um Softwaredesign und -architektur, Domain-Driven Design, C# und Windows Azure

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: