Working with GAC from code
For some day ago I was working on testing an installer that did add some files to the GAC during installation. To ensure that the installer was working correct I needed some code that could check if some files did exist, add files and remove files from the GAC. I did need this for mainly 3 test cases; files already exist in GAC, files are installed correct and repair scenario when some files are deleted. I did find a good API wrapper on Junfena Zhana’s blog that did wrap the needed COM interactions. Based on this wrapper I did create a small class that did solve my problem.
Install assembly example
public static void InstallAssembly(string assemblyPath) { if (String.IsNullOrEmpty(assemblyPath)) { throw new ArgumentNullException("assemblyPath"); } AssemblyCache.InstallAssembly(assemblyPath, null, AssemblyCommitFlags.Force); }
Working with windows firewall from code
Quite recently I did some automation for an installation program that was changing the firewalls rules. So to be able to test that the installation was working as expected I need a way to access information about the firewall from code and also a way to change the information to verify that the installer was able to repair the rules. I found a lot of smaller code snippets mostly in VB on the net with random quality. Since I’m big fan of C# I did decide to create my one snippet’s in C#
The basic functionality is found in the COM object hnetcfg.dll. So the first thing to-do is to get a firewall manager.
private const string CLSIDFireWallManager = "{304CE942-6E39-40D8-943A-B913C40C9CD4}"; private static NetFwTypeLib.INetFwMgr GetFirewallManager() { Type objectType = Type.GetTypeFromCLSID(new Guid(CLSIDFireWallManager)); INetFwMgr manager = Activator.CreateInstance(objectType) as NetFwTypeLib.INetFwMgr; if (manager == null) { throw new NotSupportedException("Could not load firewall manager"); } return manager; }
Getting the current profile The profile is used for all firewall rules interactions.
private static INetFwProfile GetCurrentProfile() { INetFwProfile profile; try { profile = GetFirewallManager().LocalPolicy.CurrentProfile; } catch (System.Runtime.InteropServices.COMException e) { throw new NotSupportedException("Could not get the current profile (COMException)", e); } catch (System.Runtime.InteropServices.InvalidComObjectException e) { throw new NotSupportedException("Could not get the current profile (InvalidComObjectException)", e); } return profile; }
Is firewall on or of?
public static bool IsWindowsFirewallOn { get { return GetCurrentProfile().FirewallEnabled; } set { GetCurrentProfile().FirewallEnabled = value; } }
My new blog
Finally I have reach the point in time when I decided to start blogging. And here is the result.