How to Unblock files received from email or the internet in C# – Programming, Pseudocode Example, C# Programming Example
C# Windows Form

How to Unblock files received from email or the internet in C#

I do a lot of technical editing. When a project editor sends me files, Windows marks them as from an “untrusted” source. Then when I try to open the files in Word, it warns me and asks if I want to open the file anyway.

This is kind of like taking candy from people. In general you shouldn’t take candy from strangers or trust files that you receive in emails. In this case, however, I know that the files are safe, so it’s just annoying.

If you right-click one of these files in File Explorer and select Properties, you’ll see something like the following picture.

The warning is at the bottom. You can unblock files one at a time by checking the Unblock box and clicking Apply. Unfortunately if you select multiple files before you display the Properties dialog, the dialog does not give the warning or display the Unblock checkbox.

Again, all of this makes sense so I’m not complaining. These are sensible safety precautions for most people. I just have an unusual situation that makes this unnecessary.

This example lets you unblock files quickly and easily. Do not use this method unless you understand it and you know that the files are safe!

The following section explains how the program lets you select and unblock files. The sections after that fill in a few details including how the listbox displays file names and how the CheckAll extension method lets you check all of the items in a checked listbox.

Unblocking Files

When you select the File menu’s Open command, the program executes the following code.

This code displays a file selection dialog that you can use to select one or more files. If you select files and click OK, the code loops through the selected files, uses each to create a new FileNameInfo object (described shortly), and adds them to the clbFiles checked listbox control. It finishes by calling the CheckAll extension method (also described shortly).

When you click the Unblock button, the following code executes.

This code loops through the checked items in the listbox. Note that the items in the listbox are FileNameInfo objects, so that’s the kind of object that the code uses to loop through the CheckedItems collection. For each checked item, the code gets the file name and calls the UnblockFile method described next.

The following code shows the UnblockFile method.

This method simply calls the DeleteFile method defined in the kernel32 library, passing it the name of the file with “:Zone.Identifier” appended. This removes the zone identifier that marks the file as blocked.

To use DeleteFile, the program must know where to find it. The following code fragment tells the program where to look.

The code imports System.Runtime.InteropServices so it can later use the DllImport and MarshalAs attributes. Inside the Form1 class, it uses those attributes to indicate where to find DeleteFile.

It’s that simple! Now you can select and unblock files en masse.

The following sections described two other useful features demonstrated by this program.

Displaying File Names

The CheckedListBox control (as well as the ListBox and ComboBox controls) display lists of objects. To determine what to display, the control uses the objects’ ToString methods. This example uses the following FileNameInfo class to represents files within the list.

This class simply stores a file’s short and full names. It has an initializing constructor that makes it easy to set those values.

It also overrides the class’s ToSrtring method so it displays the file’s short name. The listbox uses ToSrtring, so it displays the file’s short name.

If you look at the earlier code that executes when you click Unblock, you’ll see that it uses the FileNameInfo object’s FullName values to get the files’ full names.

This is a very useful technique for storing data in listbox or combo box items. Create a class that holds the data. Then override the class’s ToString method to give the listbox something to display.

Checking and Unchecking Items

For some reason, the CheckedListBox control does not have methods to check or uncheck all items. That seems like an obvious oversight, but it’s easy to fix by simply creating a extension methods.

Recall that extension methods must be static methods defined inside a static class. The following code shows this example’s extension methods.

The class defines a CheckUncheckAll method that sets all of the control’s items to either checked or unchecked. This method is an extension method because it is static and its parameter list begins with the this keyword to identify the object that it extends. Notice, however, that the method is private, so it is only accessible inside this class. This method ismply loops through the list’s items and calls the control’s SetItemChecked method to check or uncheck each item.

The CheckAll extension method is public. It calls the CheckUncheckAll method passing it the value true to check all of the control’s items.

Similarly the public UncheckAll extension method uses the CheckUncheckAll method to uncheck all of the items.

You can use similar techniques to perform other operations on listboxes. For example you could write extension methods to select/deselect all items instead of checking/unchecking them.


This example lets you quickly unblock files en masse. The key is the DeleteFile method, but you may also find the technique for storing data in listbox items useful, as well as the CheckAll and UncheckAll extension methods.

Download the example to experiment with it and to see additional details.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.