c# System.ComponentModel.Win32Exception - 找不到文件,但文件存在

在使用C#调用外部程序时,使用process进程处理,在win10上没有问题,在win7上运行时报错。

异常报错:

Exception Info: System.ComponentModel.Win32Exception
atSystem.Diagnostics.Process.StartWithShellExecuteEx(System.Diagnostics.ProcessStartinfo)
at System.Diagnostics.Process.Start0
atSystem.Diagnostics.Process.Start(System.Diagnostics.ProcessStartinfo)
at startScanFile.Program.Main(System.String[])

 win32异常捕获测试代码:

try {
System.Diagnostics.Process myProc = new System.Diagnostics.Process();
myProc.StartInfo.FileName = "c:\nonexist.exe";  //Attempting to start a non-existing executable
myProc.Start();    //Start the application and assign it to the process component.    
}
catch(Win32Exception w) {
Console.WriteLine(w.Message);
Console.WriteLine(w.ErrorCode.ToString());
Console.WriteLine(w.NativeErrorCode.ToString());
Console.WriteLine(w.StackTrace);
Console.WriteLine(w.Source);
Exception e=w.GetBaseException();
Console.WriteLine(e.Message);
}

造成异常原因:

经排查,返回异常为:System.ComponentModel.Win32Exception - ‘系统找不到指定的文件’,但文件存在。

因为win7是32位操作系统,而win10是64位,程序导包时anycpu默认64位,自然找不到文件

解决方法:

调用应用是32位的保存在win10program files(x86)里面

保存在win7的 program files里面

win10(操作系统:64位)

win7(操作系统:32位)

winform发布时打包默认anycpu,默认64位,应该改为x86发布

注:64位操作系统可兼容32

扫描二维码关注公众号,回复: 16716172 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_43801336/article/details/132753688