de_wiki_snapshot_dcd_unblock_libraries


This C# script automates the troublesome unblocking process for all the internet downloaded 3rd party developer add-ons for Grasshopper3d.

It was originally developed by our Computational Design Instructor and first shared on here.

You can learn more about why windows blocks files by following this link.

de_wiki_snapshot_dcd_unblock_libraries_01


The code snippet can be found below:

 private void RunScript(bool Run, bool Add)
 {
 #region info
 //This code contains tweaks of the following snippets:
 //Registry unblock key : http://www.thewindowsclub.com/unblock-file-windows-8
 //Unblock files pinvoke : http://stackoverflow.com/questions/6375599/is-this-pinvoke-code-correct-and-reliable
 #endregion

 #region checks
 if (Component.Params.Input[0].VolatileDataCount == 0)
 {
 Print("Connect a button component to the run input.");
 }
 if (Component.Params.Input[1].VolatileDataCount == 0)
 {
 Print("Default value is set to False / Connect a toggle component to the Add input if you want to change the default behaviour.");
 }
 #endregion

 if (Run == true)
 {
 //declare some variables
 string directory = my_userpath;
 string gh_libraries;
 string gh_user_object_lib;
 string registry_file;
 List <string> my_files;
 List <string> my_blocked_files;
 int block_counter;

 if (Add == true){
 // if true a right-click unblock option will appear in your registry
 UnblockRegistry(unb_reg(), directory, out registry_file);
 AddToRegistry(registry_file);
 File.Delete(registry_file);
 mes01 = ("Added a right-click unblock option to your local registry.Remember you have to manualy delete the key from your registry if you want to get rid of the extra option. It is located at HKEY_CLASSES_ROOT|*|shell|powershell,(delete the powershell keys).");
 }
 else{
 //else prompt for a right-click unblock option will appear in your registry
 mes01 = ("Turn the Add boolean to true to a install a right-click unblock option to your local registry.");
 }

 UnblockGHLibraries(directory, out gh_libraries, out gh_user_object_lib, out my_files, out block_counter, out my_blocked_files);
 mes = ("Found " + my_files.Count() + " add-ons in your local GH directory. Also found " + block_counter + " blocked add-ons , which will attempt to unblock.");

 }
 //output info
 Print(mes);
 Print(mes01);

 }

 // <Custom additional code> 

 //private variables messages
 private string mes;
 private string mes01;

 // get the local directory
 private string my_userpath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

 //generate the registry file to add the right-click unblock option to your windows
 private static void UnblockRegistry(string[] a, string p, out string reg_file)
 {
 string[] my_text = a;
 string my_directory = p + @"\unblock_key.reg";
 if (!File.Exists(my_directory))
 {
 System.IO.File.WriteAllLines(my_directory, my_text);
 }

 reg_file = my_directory;
 }

 //the contents of the registry file
 private static string[] unb_reg()
 {
 #region Registryvalues
 string l1 = @"Windows Registry Editor Version 5.00";
 string l2 = " ";
 string l3 = @"[HKEY_CLASSES_ROOT\*\shell\powershell]";
 string l4 = @"@=" + @"""Unblock Files""";
 string l5 = " ";
 string l6 = @"[HKEY_CLASSES_ROOT\*\shell\powershell\command]";
 string l7 = @"@=""C:\\\\Windows\\\\system32\\\\WindowsPowerShell\\\\v1.0\\\\powershell.exe Unblock-File -LiteralPath '%L'""";
 #endregion
 string[] conc = {l1,l2,l3,l4,l5,l6,l7};
 return conc;
 }

 //add the generated key to the registry (subject to admin rights)
 private static void AddToRegistry(string d)
 {
 System.Diagnostics.Process regeditProcess = System.Diagnostics.Process.Start("regedit.exe", "\"" + d + "\"");
 regeditProcess.WaitForExit();
 }

 //method to unblock all the files in the Grasshopper libraries directory
 private static void UnblockGHLibraries(string path, out string return_gh_folder, out string return_user_objects, out List <string> dir_files, out int cnt, out List <string> bl_files)
 {
 string local_gh_dir = path + @"\AppData\Roaming\Grasshopper\Libraries";
 string local_gh_user_dir = path + @"\AppData\Roaming\Grasshopper\UserObjects";
 List <string> gh_files = new List <string>();
 int block_counter = 0;
 List <String> my_bl_files = new List<string>();
 if (Directory.Exists(local_gh_dir) && Directory.Exists(local_gh_user_dir))
 {
 string [] files1 = Directory.GetFiles(local_gh_dir, "*", SearchOption.AllDirectories);
 string [] files2 = Directory.GetFiles(local_gh_user_dir, "*", SearchOption.AllDirectories);
 string [] files = files1.Concat(files2).ToArray();

 foreach(string fileName in files)
 {
 gh_files.Add(fileName);
 bool blocked = Unblocker.IsBlocked(fileName);
 if (blocked)
 {
 block_counter++;
 my_bl_files.Add(fileName);
 }

 Unblocker.UnblockFile(fileName);
 }
 }
 else if ((!Directory.Exists(local_gh_user_dir)) && (!Directory.Exists(local_gh_dir)))
 {
 throw new DirectoryNotFoundException("Are you sure you have Grasshopper installed???? Your libraries directory doesn't seem to be in the right place");
 }
 return_gh_folder = local_gh_dir;
 return_user_objects = local_gh_user_dir;
 dir_files = gh_files;
 cnt = block_counter;
 bl_files = my_bl_files;
 }

 //Unblocker public class
 public class Unblocker
 {
 //import external
 [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
 [return: MarshalAs(UnmanagedType.Bool)]
 public static extern bool DeleteFile(string name);

 public static bool IsBlocked(string Name)
 {
 bool zone_bool;
 System.Security.Policy.Zone my_zone = System.Security.Policy.Zone.CreateFromUrl(Name);
 if (my_zone.SecurityZone != System.Security.SecurityZone.MyComputer)
 {
 zone_bool = true;
 }
 else
 {
 zone_bool = false;
 }

 return zone_bool;
 }

 public static bool UnblockFile(string Name)
 {
 return DeleteFile(Name + ":Zone.Identifier");

This definition was written in GH v 0.9.0076.  Please use it “as is”, it does not come with warranties.

Please do send through edits or implementations.

Snapshot of the GH canvas below:

Unblock GH Add-ons


Use the link below to download

Download Unblock GH Libraries