思路,用electron監聽窗口事件,在監聽的回調里,運行C#代碼,獲取當前電腦的U盤路徑,獲取到就返回路徑,獲取不到就返回0
在這里卡了好久,分享出來讓大家參考
(環境沒必要和我的一樣)
我的環境:node 10.2.0
electron 7.3.1
依賴: "electron-edge-js": "^12.14.2"
代碼 :
hookWindowMessage electron 官方方法:鏈接https://www.electronjs.org/docs/api/browser-window#winhookwindowmessagemessage-callback-windows
mainWindow.hookWindowMessage(0x0219, (wParam, lParam) => {
//不要以為下面代碼中的 /**/是注釋。在edge中想要執行C#代碼 就需要 /**/包括着
var edge = require("electron-edge-js");
const findUSB=edge.func(function () {/*
using System.IO;
async (input) => {
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.DriveType == DriveType.Removable)
{
return d.Name;
}
}
return "0";
}
*/});
findUSB('0',function (error, result) {
if (error) throw error;
console.log(result);
if(result!=="0"){
console.log(fs.readdirSync(result))
}
});
})