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

你可以使用 Windows API 通过 C# 将字符串写入剪贴板。以下是一个示例代码: ```csharp using System.Runtime.InteropServices; public class ClipboardHelper { [DllImport("user32.dll", SetLastError = true)] public static extern bool OpenClipboard(IntPtr hWndNewOwner); [DllImport("user32.dll", SetLastError = true)] public static extern bool CloseClipboard(); [DllImport("user32.dll", SetLastError = true)] public static extern bool EmptyClipboard(); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem); [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes); [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr GlobalLock(IntPtr hMem); [DllImport("kernel32.dll", SetLastError = true)] public static extern bool GlobalUnlock(IntPtr hMem); [DllImport("kernel32.dll", SetLastError = true)] public static extern UIntPtr GlobalSize(IntPtr hMem); private const uint CF_UNICODETEXT = 13; public static void SetText(string text) { if (!OpenClipboard(IntPtr.Zero)) { throw new System.ComponentModel.Win32Exception(); } try { // 清空剪贴板 if (!EmptyClipboard()) { throw new System.ComponentModel.Win32Exception(); } // 分配全局内存,并锁定内存区域 int byteCount = (text.Length + 1) * 2; // 两个字节表示一个 Unicode 字符 IntPtr hGlobal = GlobalAlloc(0x2000, (UIntPtr)byteCount); // 0x2000 表示 GMEM_MOVEABLE if (hGlobal == IntPtr.Zero) { throw new System.ComponentModel.Win32Exception(); } try { IntPtr lpMem = GlobalLock(hGlobal); if (lpMem == IntPtr.Zero) { throw new System.ComponentModel.Win32Exception(); } try { // 将字符串复制到内存区域 byte[] bytes = System.Text.Encoding.Unicode.GetBytes(text); Marshal.Copy(bytes, 0, lpMem, bytes.Length); // 将内存区域设置为剪贴板数据 if (SetClipboardData(CF_UNICODETEXT, hGlobal) == IntPtr.Zero) { throw new System.ComponentModel.Win32Exception(); } // 在 Unlock 前不要释放内存,因为 SetClipboardData 后剪贴板会接管内存的控制权 } finally { GlobalUnlock(lpMem); } } catch { GlobalFree(hGlobal); throw; } } finally { CloseClipboard(); } } } ``` 在上述代码中,SetText() 方法使用 CF_UNICODETEXT 格式将字符串写入剪贴板。你可以调用 SetText("abced") 将字符串 "abced" 写入剪贴板。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值