【Electron】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,或者下图

这里写图片描述

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悠悠白云a

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值