微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.NET 进程抛出 System.InvalidOperationException

如何解决.NET 进程抛出 System.InvalidOperationException

我已经尝试了我在网上找到的所有解决方案,但都没有奏效。 我确保我已经转义了所有引号并双引号其中有空格的任何路径,我已经确保所有进程正常和正确启动。 但由于某种原因,此进程在内部失败并返回退出代码 100,并抛出此特定错误

具体的工作:

  • 函数/进程在 cmd 和 powershell 中运行良好。
  • Windows 事件日志没有说明任何关于错误的额外信息。和标题说的一样。

我尝试过的事情:

我不知道自己做错了什么,为此浪费了一天。 请有人给出比windows更好的解释...

// USING ESET
processstartinfo psi = new processstartinfo();
//Set pathway
if (System.IO.File.Exists($@"{ConfigurationManager.AppSettings["ESETPath"]}\ecls.exe"))
    psi.FileName = $@"""{ConfigurationManager.AppSettings["ESETPath"]}\ecls.exe""";
else
    psi.FileName = $@"""{ConfigurationManager.AppSettings["ESETAlternativePath"]}\ecls.exe""";
//Set arguments
psi.Arguments = $"--log-file={ConfigurationManager.AppSettings["ESETLogFilePath"]} --no-log-console --log-all {FilePath}";
psi.CreateNowindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;

//Create process with Process Info
Process proc = Process.Start(psi);
proc.WaitForExit(AllowedTimePeriod);

if (proc.HasExited == false)
{
    proc.Kill();
    throw new Exception("Error occured. Anti-Virus Scan Could not complete the operation. Scanning time period too long.");
}

//Log
string stdout = proc.StandardOutput.ReadToEnd();
string stderr = proc.StandardError.ReadToEnd();

switch (proc.ExitCode)
{
    case 0: VirusFound = false; return true;
    case 1:
        VirusFound = true;
        if (System.IO.File.Exists(FilePath))
        {
            System.IO.File.Delete(FilePath);
        }
        return true;
    default:
        throw new Exception("Error occured. Anti-Virus Scan Could not complete the operation. Returned Error Code " + proc.ExitCode.ToString());
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。