Get up to 50 % extra points for free! More info
discount week 50

Working With Our Own Files in C# 1 - Zip Archive

In the previous lesson, Use of API in C# .NET, we showed the basics of working with an API and we created a demo application.

Welcome to the first part of the series on creating files of our own type. Now you can work with many types of files, you can save and open them, download, send, …. However, imagine a real application for registering employees. Employees will have text data (first name, last name and email), a date (date of birth), number (phone number) and an image (photo). At a glance, just because of the image, it can be a problem to save all this data, ideally as a single file. Theoretically, we could choose some binary data format (due to the image) and insert text data into it. In practice, binaries are relatively inconvenient and it's hard to respond to format changes with them.


Similar applications often use archives to store data. Take, for example, MS-Word and its documents with the docx file extension. When you change the extension of any docx file to .zip, you'll find out that the document is actually a zip archive, only with a different extension. Try it.

Note: By default, Windows hides file extensions of known types, so instead of Document.docx, you only see Document. You can change the settings in the View tab -> Check File name extensions in the Show/hide section.

As an archive, a file can simply contain several files and it looks like a single file to the unsuspecting user. We'll also use a zip archive to store our employee. And instead of .zip, we set a completely different file extension, for example .employee, or if you want to stick to 3-letter extensions, just .emp.

File extensions are only used to let Windows know in which application to open a file when the user double-clicks it. Usually, each file has a dot and 3-letter extension at the end of its name. In fact, files may not have any extensions at all, may have more than one, it may be longer than 3 characters, and may not even match what is stored in the file at all. We'll disguise our .zip file as a .employee file. We won't keep .zip so it doesn't confuse the user and that the user doesn't start unpacking and changing files.

The .employee file format

The zipped folder's structure could be as follows:

  • John_Doe.employee
  • info.xml
  • foto.jpg

The Info.xml file might look like this:

        <email>[email protected]</email>

The employees element (wrapping the employee element) is here because the application can process more employees sometime in the future. Such "details" need to be considered during the design.

Creating the Employee Application

Let's create a new project, form application.

The application will contain the following form elements:

Name Type
btnLoad Button
btnChooseFile Button
btnSave Button
txtFirstName TextBox
txtLastName TextBox
txtEmail TextBox
dtpBirth DateTimePicker
pcbPhoto PictureBox

You can add labels to the elements.

Choosing an image

Double-click on the Choose image button (you can see the button in the image above). A Click event handler is created.

We'll create a new OpenFileDialog in the event handler. We'll set JPG, PNG and BMP suffixes as a suffix filter. We write the filter by typing a label that appears in the dialog first. Then by separating with | (Right Alt + W) and typing *.suffix and then by separating again with | and writing another supported extension. The result is quite confusing, but what can we do. For our example, this is enough:

Photo JPG (*.jpg)|*.jpg|Photo PNG (*.png)|*.png|Photo BMP (*.bmp)|*.bmp

We'll open the dialog and compare its return value. If OK, the user has selected an image. If anything else, the user has either selected nonsense or canceled the window. Once the user selects the image, we'll load it to the employee and call an UpdateFormData method, which we'll create later.

private void btnChoosePhoto_Click(object sender, EventArgs e)
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = "Photo JPG (*.jpg)|*.jpg|Photo PNG (*.png)|*.png|Photo BMP (*.bmp)|*.bmp";
    if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
        employee.Photo = Image.FromFile(dialog.FileName);

The Employee Class

Let's start with what should be clear - properties. Our employee will have a FirstName, LastName, an Email, a Phone, DateOfBirth and also a photo.

class Employee
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public DateTime DateOfBirth { get; set; }
    public Image Photo { get; set; }

Our first constructor (we'll add more when we need them) will create a new empty employee. It does nothing but assign empty values to the properties.

public Employee()
    FirstName = "";
    LastName = "";
    Email = "";
    Phone = "";
    DateOfBirth = DateTime.Now;

Finally, create an employee instance in the form as an attribute of the form class.

private Employee employee = new Employee();

Now we've designed the application, next time we'll finally look at the actual processing of ZIP files and for the first time we'll export and import employees.



By downloading the following file, you agree to the license terms

Downloaded 3x (44.37 kB)
Application includes source codes in language C#


Previous article
Use of API in C# .NET
All articles in this section
Files and I/O in C# .NET
Article has been written for you by Filip Smolík
User rating:
No one has rated this quite yet, be the first one!




To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!