如何解决.NET 进程抛出 System.InvalidOperationException
我已经尝试了我在网上找到的所有解决方案,但都没有奏效。 我确保我已经转义了所有引号并双引号其中有空格的任何路径,我已经确保所有进程正常和正确启动。 但由于某种原因,此进程在内部失败并返回退出代码 100,并抛出此特定错误。
具体的工作:
我尝试过的事情:
-
所以最初我认为这与用户访问 C 驱动器有关。虽然由于 VS 是在 admin 中运行,我认为这没问题。但是我查看了此链接:https://forum.eset.com/topic/14598-cant-scan-c-drive/ 没有帮助。
-
然后我发现了一堆相似甚至相同的问题;他们中的很多人导致该过程实际上没有正确启动的解决方案:https://social.msdn.microsoft.com/Forums/vstudio/en-US/e899c6c0-7fba-40ec-a824-1bb26715318f/processstart-invalidoperationexception?forum=csharpgeneral 但是,这并没有提供我的解决方案。
-
与上述声明类似,我发现了一个旧答案,该答案暗示 processstartinfo 对象的某些属性设置不正确 https://forums.codeguru.com/showthread.php?517237-Process-started-in-dll-works-correctly-in-testing-but-not-in-final-version 但是在更改 processstartinfo 的方式时,这对我也没有任何作用设置的属性。
- 设置 processstartinfo.WorkingDirectory 和 processstartinfo.FileName,反之亦然。
-
我还假设 ecls.exe 命令行可执行文件实际上无法像带有属性标签的 cmd 行 exe 一样运行(因为原始命令只是称为“ecls”而不是“ecls.exe”,所以测试了设置 processstartinfo 以启动空白 CMD 控制台然后运行原始扫描 cmd 的方法。但是这最终抛出了相同的错误,所以我知道 ecls.exe 可以以 cmd 行方式运行;它只是中断别的。
-
我也终于找到了至少给了我一些提示的东西:https://www.experts-exchange.com/questions/29134987/ASP-NET-gets-InvalidOperationException-exception-on-System-Diagnostic-Process-object.html 解决方案是在付费墙后面,但我注意到在这个问题中,他们实际上达到了某种输出,而我的问题甚至从未达到他们收到的输出。所以我知道它在 exe 运行时失败,而不是在 exe 中失败。 使用 StandardOutput 错误日志记录后,我将确切的问题缩小到抛出的这个错误:
“无法打开 C:\Users\...\Logs\scanner.log: ????????????\r\n”
我不知道自己做错了什么,为此浪费了一天。 请有人给出比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 举报,一经查实,本站将立刻删除。