{"record":{"id":"e82fbb12e3cc799e","repo":"ZyperWave/ZyperWinOptimize","slug":"cmd-ex-message","errorCode":null,"errorMessage":"执行CMD文件时出错: {ex.Message}","messagePattern":"执行CMD文件时出错: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"ZyperWin++/ZyperWin++/Activate.cs","lineNumber":88,"sourceCode":"                        process.StartInfo.WorkingDirectory = Path.GetDirectoryName(filePath);\r\n\r\n                        process.Start();\r\n\r\n                        // 读取输出（可选）\r\n                        string output = process.StandardOutput.ReadToEnd();\r\n                        string error = process.StandardError.ReadToEnd();\r\n\r\n                        process.WaitForExit();\r\n\r\n                        if (process.ExitCode != 0)\r\n                        {\r\n                            throw new Exception($\"CMD执行失败，退出码: {process.ExitCode}\\n错误信息: {error}\");\r\n                        }\r\n                    }\r\n                }\r\n                catch (Exception ex)\r\n                {\r\n                    throw new Exception($\"执行CMD文件时出错: {ex.Message}\");\r\n                }\r\n            });\r\n        }\r\n    }\r\n}\r\n","sourceCodeStart":70,"sourceCodeEnd":94,"githubUrl":"https://github.com/ZyperWave/ZyperWinOptimize/blob/d20e78bbd906fa179e4cc9f5107b502da5967aa9/ZyperWin++/ZyperWin++/Activate.cs#L70-L94","documentation":"ExecuteCmdFile wraps every failure inside its Task.Run body — including the exit-code throw, process-start failures, and IO errors — and rethrows it as \"执行CMD文件时出错: {ex.Message}\". This wrapper is what propagates to button1_Click, which shows it in a MessageBox.","triggerScenarios":"Any exception in the cmd.exe invocation path: the inner \"CMD执行失败，退出码...\" exception, cmd.exe unable to start, WorkingDirectory (Bin folder) missing so the process cannot launch, or redirected stream read failures.","commonSituations":"App deployed without the Bin directory so the working directory is invalid; AV terminating cmd.exe mid-run; the same non-zero exit code from error [0] double-wrapped by this handler; path issues after moving the exe.","solutions":["Look past the wrapper at the inner message (after the colon) to find the real cause.","Confirm Application.StartupPath\\Bin exists and contains MAS_AIO_CN.cmd before invoking.","Run the cmd file manually from an elevated prompt to reproduce and see the raw error.","Keep the original exception as InnerException so exit codes are not flattened into a string."],"exampleFix":"// before\ncatch (Exception ex) { throw new Exception($\"执行CMD文件时出错: {ex.Message}\"); }\n// after\ncatch (Exception ex) { throw new Exception(\"执行CMD文件时出错\", ex); } // preserve inner exception + stack","handlingStrategy":"try-catch","validationCode":"string binDir = Path.Combine(Application.StartupPath, \"Bin\");\nif (!Directory.Exists(binDir) || !File.Exists(Path.Combine(binDir, \"MAS_AIO_CN.cmd\")))\n{ MessageBox.Show(\"Bin 目录或脚本缺失，无法执行\"); return; }","typeGuard":null,"tryCatchPattern":"try { await ExecuteCmdFile(path); }\ncatch (Exception ex)\n{ var inner = ex.InnerException ?? ex; MessageBox.Show($\"执行CMD文件时出错: {inner.Message}\", \"错误\", MessageBoxButtons.OK, MessageBoxIcon.Error); }","preventionTips":["Always preserve InnerException when wrapping.","Validate the working directory exists before Process.Start.","Run the packaged app from a complete extraction (check first run after download).","Test the cmd path on a clean VM with default Defender settings."],"tags":["exception-wrapper","process-exit-code","cmd-script","windows"],"backgroundTag":"command-not-found","analyzedSha":"d20e78bbd906fa179e4cc9f5107b502da5967aa9","analyzedAt":"2026-09-13T15:15:37.168Z","contentChangedAt":"2026-09-13T15:15:37.168Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}