ZyperWave/ZyperWinOptimize · error · Exception

执行CMD文件时出错

Error message

执行CMD文件时出错: {ex.Message}

What it means

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.

Solutions

  1. Look past the wrapper at the inner message (after the colon) to find the real cause.
  2. Confirm Application.StartupPath\Bin exists and contains MAS_AIO_CN.cmd before invoking.
  3. Run the cmd file manually from an elevated prompt to reproduce and see the raw error.
  4. Keep the original exception as InnerException so exit codes are not flattened into a string.

Example fix

// before
catch (Exception ex) { throw new Exception($"执行CMD文件时出错: {ex.Message}"); }
// after
catch (Exception ex) { throw new Exception("执行CMD文件时出错", ex); } // preserve inner exception + stack
Defensive patterns

Strategy: try-catch

Validate before calling

string binDir = Path.Combine(Application.StartupPath, "Bin");
if (!Directory.Exists(binDir) || !File.Exists(Path.Combine(binDir, "MAS_AIO_CN.cmd")))
{ MessageBox.Show("Bin 目录或脚本缺失,无法执行"); return; }

Try / catch

try { await ExecuteCmdFile(path); }
catch (Exception ex)
{ var inner = ex.InnerException ?? ex; MessageBox.Show($"执行CMD文件时出错: {inner.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of ZyperWave/ZyperWinOptimize@d20e78bbd9 (2026-09-13). Data as JSON: /api/errors/e82fbb12e3cc799e. Report an issue: GitHub.

Appendix: source

Thrown at ZyperWin++/ZyperWin++/Activate.cs:88

                        process.StartInfo.WorkingDirectory = Path.GetDirectoryName(filePath);

                        process.Start();

                        // 读取输出(可选)
                        string output = process.StandardOutput.ReadToEnd();
                        string error = process.StandardError.ReadToEnd();

                        process.WaitForExit();

                        if (process.ExitCode != 0)
                        {
                            throw new Exception($"CMD执行失败,退出码: {process.ExitCode}\n错误信息: {error}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception($"执行CMD文件时出错: {ex.Message}");
                }
            });
        }
    }
}

View on GitHub (pinned to d20e78bbd9)