Zipping files in C#
For a while ago I tried to create zip files to be used by windows built in zip functionality. Did try to use GZipStream but I found out that I needed a GZip expander installed to be able to use it. I don’t understand why we have an inbuilt function in VS to create a compressed format that Windows can’t open natively. Anyway after a lot of search I found DotNetZip. It has a really easy to use API and no dependencies to other DLL’s.
It supports the following scenarios
-creating a zip archive, adding files or directories into the archive
-extracting files from an existing archive
-modifying an existing archive – removing entries from an archive or adding new entries to an archive
-password protecting entries in the archive
-getting entry input from a file or a stream
-reading a zip file from a file or a stream
-extracting an entry into a file or a stream
Example Usage
try { using (ZipFile zip = new ZipFile("MyZipFile.zip") { zip.AddFile("c:\photos\personal\7440-N49th.png"); zip.AddFile("c:\Desktop\2005_Annual_Report.pdf"); zip.AddFile("ReadMe.txt"); zip.Save(); } } catch (System.Exception ex1) { System.Console.Error.WriteLine("exception: " + ex1); } |
How to delete a element in an array
This problem sounds where simple, but it did take me a lot of effort to figure out that PHP doesn’t have any really good functionallity for it. So did create my own version.
<?php function in_array_delete($array, $item) { if (isset($array[$item])) { unset($array[$item]); } return array_merge($array); } ?> |
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.