{"record":{"id":"aed3f43922de6a82","repo":"BCUninstaller/Bulk-Crap-Uninstaller","slug":"win32-directory-compress-returned-ret","errorCode":null,"errorMessage":"Win32_Directory.Compress returned {ret}","messagePattern":"Win32_Directory\\.Compress returned (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"source/KlocTools/Tools/FilesystemTools.cs","lineNumber":125,"sourceCode":"                Directory.Move(source.FullName, target.FullName);\n            else\n            {\n                CopyRecursive(source, target);\n                source.Delete(true);\n            }\n        }\n\n        public static void CompressDirectory(string dirFullName) => CompressDirectory(dirFullName, ManagementOptions.InfiniteTimeout);\n        public static void CompressDirectory(string dirFullName, TimeSpan timeout)\n        {\n            var objPath = \"Win32_Directory.Name=\" + \"\\\"\" + dirFullName.Replace(@\"\\\", @\"\\\\\") + \"\\\"\";\n            using (var dir = new ManagementObject(objPath))\n            {\n                var outParams = dir.InvokeMethod(\"Compress\", null, new InvokeMethodOptions { Timeout = timeout });\n                if (outParams == null) throw new ArgumentNullException(nameof(outParams));\n                var ret = (uint)outParams.Properties[\"ReturnValue\"].Value;\n                if (ret != 0)\n                    throw new IOException(\"Win32_Directory.Compress returned \" + ret);\n            }\n        }\n\n        [DllImport(\"shlwapi.dll\")]\n        public static extern bool PathIsNetworkPath(string pszPath);\n\n        [DllImport(\"kernel32.dll\", EntryPoint = \"CreateSymbolicLinkW\", CharSet = CharSet.Unicode)]\n        private static extern int CreateSymbolicLink([In] string lpSymlinkFileName, [In] string lpTargetFileName,\n            SymbolicLinkType dwFlags);\n    }\n}","sourceCodeStart":107,"sourceCodeEnd":136,"githubUrl":"https://github.com/BCUninstaller/Bulk-Crap-Uninstaller/blob/608321de98e92297377b1eb69029af55c25504a1/source/KlocTools/Tools/FilesystemTools.cs#L107-L136","documentation":"Thrown by FilesystemTools.CompressDirectory after invoking the WMI method Win32_Directory.Compress. It casts outParams.Properties[\"ReturnValue\"].Value to uint and, if it is non-zero, throws IOException with the code appended. Non-zero return codes from this WMI method indicate failure (e.g. access denied, path not found, the directory is on a volume that does not support NTFS compression).","triggerScenarios":"Calling CompressDirectory on a path that does not exist, is on a non-NTFS volume (FAT32/exFAT), when the process lacks SeManageVolumePrivilege / admin rights, or when WMI is broken/disabled.","commonSituations":"Portable apps run from FAT32 USB drives, non-elevated processes, WMI repository corruption, or paths with characters that break the WMI object path escaping.","solutions":["Ensure the target volume is NTFS (or ReFS where supported) before compressing.","Run the host process elevated and confirm WMI service (winmgmt) is healthy.","Decode the ReturnValue (e.g. 2 = access denied, 3 = path not found) and surface a meaningful error; fall back to the Win32 DeviceIoControl FSCTL_SET_COMPRESSION P/Invoke if WMI is unreliable."],"exampleFix":"// before\nFilesystemTools.CompressDirectory(@\"E:\\MyDir\"); // E: is FAT32 -> code 11\n\n// after: guard against non-NTFS and report the code\nif (new DriveInfo(Path.GetPathRoot(dir)).DriveFormat != \"NTFS\")\n    throw new InvalidOperationException(\"Compression requires an NTFS volume.\");\ntry { FilesystemTools.CompressDirectory(dir); }\ncatch (IOException ex) { logger.Error($\"Compress failed: {ex.Message}\"); }","handlingStrategy":"try-catch","validationCode":"var root = Path.GetPathRoot(dirFullName);\nif (new DriveInfo(root).DriveFormat != \"NTFS\") return; // skip non-NTFS\nFilesystemTools.CompressDirectory(dirFullName);","typeGuard":null,"tryCatchPattern":"try { FilesystemTools.CompressDirectory(dir); }\ncatch (IOException ex) { logger.Warn($\"Compress failed ({ex.Message}). Code may indicate access/path/FS error.\"); }","preventionTips":["Verify the target volume is NTFS before attempting compression.","Run elevated and confirm WMI health on target machines.","Decode ReturnValue (2 access denied, 3 not found) for actionable errors."],"tags":["wmi","filesystem","compression","windows","csharp","kloctools"],"backgroundTag":null,"analyzedSha":"608321de98e92297377b1eb69029af55c25504a1","analyzedAt":"2026-08-13T12:17:35.389Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}