Node和Electron開發入門(四):操作PC端文件系統


一、調用PC端默認方式打開本地文件
在main.js里

 // 打開系統本地文件或者網頁鏈接 const {shell} = require('electron'); // Open a local file in the default app var path1 = "d:\\ProjectsSpace\\ElectronProjects\\ElectronTest2\\app\\html\\config\\record.txt"; shell.openItem(path1); // Open a URL in the default way shell.openExternal('https://github.com');

 

這個使用的是electron里shell模塊的方法,只能打開和刪除,不能通過代碼更改文件內容,具體請看shell的api http://electron.atom.io/docs/api/shell/

二、通過代碼讀取、寫入、更名、刪除、遍歷目錄、鏈接等 POSIX 文件系統操作
這個使用的是node.js里的fs 模塊,在electron中使用,案例代碼如下:
在main.js里

//本地文件寫入 var path = require('path'); var _path = path.join(__dirname, '..', '\\app\\html\\config\\record.txt'); var path1 = "d:\\ProjectsSpace\\ElectronProjects\\ElectronTest2\\app\\html\\config\\record.txt"; console.log(_path, path1);//測試路徑對不對的 var fs = require('fs'); fs.readFile(_path, 'utf8', function (err, data) { if (err) return console.log(err); }); fs.writeFile(_path, "electron + Javascript", function (err) { if (!err) console.log("寫入成功!") })

 

關於fs更詳細的使用方法,請看相關api,或者下圖

這里寫圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM