最近使用 C# 調用 dll 中的 UpdateDriverForPlugAndPlayDevices() 方法去安裝驅動,遇到了 “The system cannot find the file specified” 問題,
看着錯誤提示,是指系統沒有找到指定文件,但是我檢查硬盤,該文件確實存在,但是因為我測試時使用的是 Hebrew 希伯來語的系統,
我就考慮會不會是因為路徑中包含了特殊字符,導致該方法不能正常工作,我當前的方法聲明如下:
[DllImport("newdev.dll", SetLastError = true)] public static extern bool UpdateDriverForPlugAndPlayDevices(IntPtr hwndParent, string HardwareId, string FullInfPath, UpdateDriverInstallFlag install, out Boolean bRebootRequired);
后面經查資料,添加了 CharSet = CharSet.Auto,問題解決, 修改后的方法聲明:
[DllImport("newdev.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern bool UpdateDriverForPlugAndPlayDevices(IntPtr hwndParent, string HardwareId, string FullInfPath, UpdateDriverInstallFlag install, out Boolean bRebootRequired);