node操作 windows的appdata本地缓存文件
const os = require('os');
const path = require("path");
const fs = require("fs"); var homedir = os.homedir(); function mkdirs(dirpath) {
if (!fs.existsSync(path.dirname(dirpath))) {
mkdirs(path.dirname(dirpath));
}
fs.mkdirSync(dirpath);
} function createDir(myPath){
fs.existsSync(myPath) == false && mkdirs(myPath);
} var _x = Symbol("x"); class AppData{
constructor(dbnm){
if(!dbnm){
throw new Error("the database name is needless");
return;
}
dbnm = dbnm + ".info";
this.apppath = path.join(homedir,"/AppData/Local/excelMaster/", dbnm);
createDir(path.dirname(this.apppath)); }
connect(cd){
cd = cd || function(){};
fs.readFile(this.apppath,"utf-8",function(err,res){
if(err){
this[_x] = {};
}else{
var str = Buffer.from(res, 'base64').toString("utf8");
if(str){
try{
this[_x] = JSON.parse(str);
}catch(e){
this[_x] = {};
}
}else{
this[_x] = {};
}
}
cd(err,res);
});
}
connectSync(){
try{
var res = fs.readFileSync(this.apppath,"utf-8");
var str = Buffer.from(res, 'base64').toString("utf8");
if(str){
try{
this[_x] = JSON.parse(str);
}catch(e){
this[_x] = {};
}
}else{
this[_x] = {};
}
}catch(e){
this[_x] = {};
} }
get(k){
return this[_x][k];
}
set(k,val,callback){
callback = callback || function(){};
this[_x][k] = val;
const buf = Buffer.from(JSON.stringify(this[_x]), 'utf8');
fs.writeFile(this.apppath,buf.toString('base64'),callback);
}
setSync(k,val){
this[_x][k] = val;
const buf = Buffer.from(JSON.stringify(this[_x]), 'utf8');
fs.writeFileSync(this.apppath,buf.toString('base64'));
}
setSyncObj(obj){
for(var i in obj){
this[_x][i] = obj[i];
}
const buf = Buffer.from(JSON.stringify(this[_x]), 'utf8');
fs.writeFileSync(this.apppath,buf.toString('base64'));
}
has(k){
return k in this[_x];
}
keys(){
return Object.keys(this[_x]);
}
values(){
return Object.values(this[_x]);
}
drop(){
this[_x] = {};
const buf = Buffer.from(JSON.stringify({}), 'utf8');
fs.writeFileSync(this.apppath,buf.toString('base64'));
}
disconnect(){
this.apppath = null;
delete this[_x];
}
} module.exports = AppData;
使用方式
var AppData = require("./appdata"); var p = new AppData("abc"); p.connectSync(); //p.setSync("manny","28"); console.log(p.get("manny")) p.disconnect();
node操作 windows的appdata本地缓存文件的更多相关文章
- Windows下搭建本地SVN服务器【转】
转自:http://www.linuxidc.com/Linux/2015-01/111563.htm 本文介绍Windows下搭建本地SVN服务器的方法,网上资料比较少也比较旧,大都介绍的是旧版本S ...
- 强势解决:windows 不能在本地计算机中起动Tomcat参考特定错误代码1
Tomcat添加系统服务:service.bat install 启动本服务的时候却提示“windows 不能在本地计算机中起动 Apache Tomcat参考特定错误代码1,若不是Microsoft ...
- Windows不能在本地计算机启动OracleDBConsoleorcl .错误代码2
Windows 不能在 本地计算机 启动 OracleDBConsoleorcl.有关更多信息,查阅系统事件日志.如果这是非 Microsoft 服务,请与服务厂商联系,并参考特定服务错误代码 2. ...
- Windows 不能在 本地计算机 启动 SQL Server 服务 错误代码126
本文转自:http://www.cnblogs.com/yuerdongni/archive/2012/08/18/2645140.html 在使用SQL2005(或2008)是可能会遇到错误提示: ...
- windows不能在本地计算机启动SQL Server(MSSQLSERVER)
windows不能在本地计算机启动sql server 在登录数据库的时候,发现数据库不能登录,提示[无法连接到实例],很明显这是因为数据库服务没有启动导致的,我们打开[服务]启动相应的SQL数据库服 ...
- 【转】解决Windows不能在本地计算机启动apache tomcat
http://blog.163.com/ftskwsg@126/blog/static/5623853020094494117827/ 这个方法解决了我的问题. 在windows下以服务的方式启动时提 ...
- winreg操作windows注册表详解示例
#coding:utf-8 #=====================================================================#=====本程序演示了WINR ...
- C语言操作WINDOWS系统存储区数字证书相关函数详解及实例
C语言操作WINDOWS系统存储区数字证书相关函数详解及实例 以下代码使用C++实现遍历存储区证书及使用UI选择一个证书 --使用CertOpenSystemStore打开证书存储区. --在循环中 ...
- C# 操作windows服务[启动、停止、卸载、安装]
主要宗旨:不已命令形式操作windows服务 static void Main(string[] args) { var path = @"E:\开发辅助项目\WCF\WCF.Test\WC ...
随机推荐
- 在AJAX里 使用【 XML 】 返回数据类型 实现简单的下拉菜单数据
在AJAX里 使用XML返回数据类型 实现简单的下拉菜单数据 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...
- How to add the ApplicationPoolIdentity to a SQL Server Login
The ApplicationPoolIdentity is a virtual account in Windows that is dynamically generated when the a ...
- 项目报错java.lang.ClassNotFoundException: org.common.SessionListener
现象:项目报错java.lang.ClassNotFoundException: org.common.SessionListener,并且myeclipse左侧Package Explorer中项目 ...
- 所生成项目的处理器架构“MSIL”与引用“***”的处理器架构“x86”不匹配。这种不匹配可能会导致运行时失败。请考虑通过配置管理器...
警告:所生成项目的处理器架构“MSIL”与引用“***”的处理器架构“x86”不匹配.这种不匹配可能会导致运行时失败.请考虑通过配置管理器更改您的项目的目标处理器架构,以使您的项目与引用间的处理器架构 ...
- SQL Server 2012 安装过程详解(包含每一步设置的含义)
转http://www.cnblogs.com/EastLiRoar/p/4051969.html 一.启动安装程序,点击“安装”选项卡,选择“全新SQL Server独立安装或向现有安装添加功能”. ...
- POJ 2442 - Sequence - [小顶堆][优先队列]
题目链接:http://poj.org/problem?id=2442 Time Limit: 6000MS Memory Limit: 65536K Description Given m sequ ...
- [No0000117]visual studio 调试WebForm 显示 HTTP Error 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容。
调试界面如下: 解决办法1:右键设置起始页. 影响文件: 解决方案2:Web.config中添加默认页面配置: <system.webServer> <defaultDocument ...
- Exception 05 : Could not instantiate id generator
异常名称: Could not instantiate id generator 异常截图: 异常原因:Sequence不支持mysql数据库 Sequence支持的是有序列的数据库,此时可以将ora ...
- Isomorphism 同构
小结: 1.两个有限维度的向量空间,在同一数域下,是同构的 等价于 它们维数相等. Isomorphism 同构 0.1.8 Isomorphism. If U and V are vector sp ...
- PHP之魔术引号
什么是魔术引号 Warning 本特性已自 PHP5.3.0起废弃并将自PHP5.4.0起移除. 当打开,所有的'(单引号),"(双引号), (反斜线)和NULL 字符都会被自动加上一个反 ...