Not logged in - Login

Create a Compressed Folder

The following is an example of how to create a folder and set it to be compressed. This is NOT the same as creating a zip file.

public void CreateCompressedFolder(string strFolderName)
   // to use this, add System.Management to the references.

   // this is the folder to be created as a compressed folder.
   // double up the '\' characters.
   string strMgmPath = strFolderName.Replace(@"\", @"\\");

   // create the folder, if it doesn't already exist
   if (!System.IO.Directory.Exists(strFolderName))
      // create the folder

   // build the object path that we'll need for the Management Object.
   string strMgmObjPath = "Win32_Directory.Name=" + "\"" + strMgmPath + "\"";

   // create an instance of our System Management Object inside a Using() block so it will get disposed.
   using (System.Management.ManagementObject objMgm = new System.Management.ManagementObject(strMgmObjPath))
      // compress the folder
      System.Management.ManagementBaseObject outParams = objMgm.InvokeMethod("Compress", null, null);

      // determine the results of the compress operation
      uint ret = (uint)(outParams.Properties["ReturnValue"].Value);

      if (ret != 0)
         throw new System.Exception(string.Format("CreateCompressedFolder() failed with error code: {0}", ret));