Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unblock File from within .net 4 c#

Is there a possibility to unblock a file that is downloaded from the internet from within a c# program. Surfing the internet I have learned, that the information is written in an alternative stream of a (NTFS) file that contains the current zone information (value 3 is from the internet and is interpreted as blocked).

Is there a managed possiblity to either clear or change the zone information (unblock) of a file or is there a managed copy function that copies the files without the zone information? If not, how can I do with PInvoke but without including a foreign assembly (I'm not allowed to do this in a current project).

like image 991
HCL Avatar asked Jun 16 '11 15:06

HCL


People also ask

How do I unblock a file in properties?

Open Microsoft Windows Explorer to locate and right-click the downloaded file, choose Properties from the context menu. Click the Unblock button in the lower right-hand corner of the resulting dialog. Click OK or Apply.

How do I unblock a DLL file?

You just have to open file properties and click on Unblock button. Unblock the zip file first, and then extract the dll, unblock the dll if needed: Other tricks is to copy the file to a file system that doesn't support alternate data streams, that slices them off the file. A flash drive for example.


1 Answers

Based on your input I have done the following code:

public class FileUnblocker {     [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]     [return: MarshalAs(UnmanagedType.Bool)]     private static extern bool DeleteFile(string name);      public bool Unblock(string fileName) {         return DeleteFile(fileName + ":Zone.Identifier");     } } 

Thanks to Stuart Dunkeld, Alex K(+1) and Sven to show me the direction.

UPDATE I have posted the code here for a feedback if it would work reliable in production environment. If someone want to use it, check out there.

like image 162
HCL Avatar answered Oct 14 '22 15:10

HCL