{"record":{"id":"7c7897c344db0b1c","repo":"peass-ng/PEASS-ng","slug":"error-already-exists","errorCode":"ERROR_ALREADY_EXISTS","errorMessage":"({183}) Cannot create a file when that file already exists: [{junctionPath}]","messagePattern":"\\((.+?)\\) Cannot create a file when that file already exists: \\[(.+?)\\]","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/Directory Class/Directory Core Methods/Directory.CreateJunctionCore.cs","lineNumber":105,"sourceCode":"\n\n         // Check for existing directory junction folder.\n         if (File.ExistsCore(transaction, true, junctionPath, pathFormat))\n         {\n            if (overwrite)\n            {\n               DeleteDirectoryCore(transaction, null, junctionPath, true, true, true, pathFormat);\n\n               CreateDirectoryCore(true, transaction, junctionPath, null, null, false, pathFormat);\n            }\n\n            else\n            {\n               // Ensure the folder is empty.\n               if (!IsEmptyCore(transaction, junctionPath, pathFormat))\n                  throw new DirectoryNotEmptyException(junctionPath, true);\n\n               throw new AlreadyExistsException(junctionPath, true);\n            }\n         }\n\n\n         // Create the folder and convert it to a directory junction.\n         CreateDirectoryCore(true, transaction, junctionPath, null, null, false, pathFormat);\n\n         using (var safeHandle = OpenDirectoryJunction(transaction, junctionPath, pathFormat))\n            Device.CreateDirectoryJunction(safeHandle, directoryPath);\n\n\n         // Copy the target date and time stamps to the directory junction.\n         if (copyTargetTimestamps)\n            File.CopyTimestampsCore(transaction, true, directoryPath, junctionPath, true, pathFormat);\n\n\n         return junctionPath;\n      }","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/Directory Class/Directory Core Methods/Directory.CreateJunctionCore.cs#L87-L123","documentation":"Directory.CreateJunctionCore throws AlreadyExistsException (Win32 error 183, ERROR_ALREADY_EXISTS) when the junction path already exists as an EMPTY directory (or is otherwise already present) and cannot be created again. The existence branch first checks IsEmptyCore; if the folder is empty it falls through to AlreadyExistsException rather than attempting creation. It means 'you asked me to create something that is already there'.","triggerScenarios":"Calling Directory.CreateJunction / CreateJunctionTransacted with a junctionPath that already exists as an empty directory. Also occurs on any re-invocation without checking existence first, since CreateDirectoryCore would hit ERROR_ALREADY_EXISTS.","commonSituations":"Idempotency mistakes: setup scripts run twice; a junction was created by a previous run or another installer; unit tests that do not clean up fixtures between runs.","solutions":["Check Directory.Exists(junctionPath) before creating and skip creation if it already exists.","Delete the existing empty directory first, then call CreateJunction.","Catch AlreadyExistsException and treat it as a success/no-op if a junction at that path is acceptable.","Use Directory.GetLinkTargetInfo to verify the existing item is already the junction you want before deciding to recreate it."],"exampleFix":"// before\nDirectory.CreateJunction(@\"C:\\link\", @\"D:\\target\"); // throws if C:\\link exists\n// after\nif (!Directory.Exists(linkPath))\n    Directory.CreateJunction(linkPath, targetPath);","handlingStrategy":"validation","validationCode":"if (Directory.Exists(junctionPath))\n    return; // already created; skip or verify it points at the right target","typeGuard":null,"tryCatchPattern":"try { Directory.CreateJunction(junctionPath, targetPath); }\ncatch (AlreadyExistsException) { /* treat as no-op if re-runs are expected */ }","preventionTips":["Guard creation with Directory.Exists before calling","Design installers/scripts to be idempotent","Clean up junctions in teardown of tests and uninstalls","Verify existing link target with GetLinkTargetInfo before recreating"],"tags":["windows","filesystem","junction","already-exists"],"backgroundTag":"path-already-exists","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}