Windows 小工具可以保存数据吗?

Can a Windows Gadget save data?

我正在创建一个小 Windows 侧边栏小工具,用于在简单的 textarea.

中做笔记

像往常一样,我有一个 gadget.xml 清单文件和一个 .html 文件,见下文。

如何在 Gadget 中读取一些数据/保存一些数据?

我知道仅使用 JavaScript 通常是不可能的(注意:使用 localstorage 是不可能的,因为我想要持久化数据 ),所以 如何在Windows Gadget中save/read数据?


<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=Unicode" />
        <title>NeverForget</title>
        <style type="text/css">
        body
        {
            margin: 0;
            width: 300px;
            height: 200px;            
            background-color: transparent;
        }
        #gadgetContent 
        {
            width: 100%;
            height: 100%;
            overflow: hidden;
            border: none;
            background-color: transparent;
        }
        </style>
        <script type="text/jscript" language="jscript">
            function init() {
                // how to load notes from a file here on startup?
            }
            window.onkeydown = function () {
                // how to save data to file?
            }
        </script>
    </head>

    <body onload="init()">
            <textarea id="gadgetContent">Bonjour</textarea>
    </body>
</html>

尝试以下方法之一:

  1. 有内置的 methods of the System.Gadget.Settings object 可用于从 Settings.ini 文件(存储在 C:\Users\[user]\AppData\Local\Microsoft\Windows侧边栏)但如果关闭或卸载小工具,此信息将丢失。

  2. 使用FileSystemObject在任何地方创建或读取或写入folders/files。限制:文件只能保存为unicode或ascii。

  3. 使用 ADO Stream object 在任何地方创建或读取或写入文件。限制:无法创建文件夹 - 必须与 FileSystemObject 结合使用。优点:可以使用您计算机上存在的任何代码页。

由于我喜欢用 utf-8 保存文本文件,下面的示例使用了第三种方法,但您可能决定放弃一些错误处理。 (注意 - 此示例基于 script published by Andrew Urquhart

幸运的是,侧边栏可以使用 knownfolders and knownfolderpaths,因此找到您的文档文件夹的路径就像

一样简单
var docs = System.Shell.knownFolderPath("Documents");

请记住,反斜杠是 javascript 中的转义字符,因此字符串中的路径必须将其反斜杠加倍。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>NeverForget</title>
<style type="text/css">
<!--
body {margin:0;width:300px;height:200px;background-color:transparent;padding:10px;}
#gadgetContent {width:280px;height:144px;overflow:auto;border:1px solid black;background-color:#eee;}
button {margin-left:66px;margin-top:10px;}
#message {display:none;width:280px;height:180px;position:absolute;top:10px;left:10px;background-color:#eee;border:1px solid red;}
#messageContent {width:278px;height:144px;word-wrap:break-word;overflow-y:auto;}
#newButton {position:absolute;bottom:10px;left:54px;}
-->
</style>
<script type="text/jscript">
//globals
var myFolderPath=System.Shell.knownFolderPath("Documents")+"\myFiles";
//end globals
function showMessage(msg){
 message.style.display="block";
 messageContent.innerText=msg;
}
function closeMessage(){
 message.style.display="none";
 messageContent.innerText="";
}
function loadFile(strAbsoluteFilePath, strCharSet){
 var adReadAll=-1, adReadLine=-2, strFileContents="", objStream=new ActiveXObject("ADODB.Stream"), fso=new ActiveXObject("Scripting.FileSystemObject");
 try{
  if(!strAbsoluteFilePath){
   throw new Error(1, "Required parameter \"strAbsoluteFilePath\" was not defined");
  }
  if(!strCharSet){
   throw new Error(2, "Required parameter \"strCharSet\" was not defined");
  }
  if(!fso.FolderExists(myFolderPath)){
   throw new Error(3, "Folder \""+myFolderPath+"\" does not exist");
  }
  objStream.Open();
  try{
   objStream.CharSet=strCharSet;
   objStream.LoadFromFile(strAbsoluteFilePath);
   strFileContents=objStream.ReadText(adReadAll);
   gadgetContent.innerText=strFileContents;
  }
  catch(err){
   throw new Error(err.number, "Loading failed:\r\n" + err.description);
  }
  finally{
   objStream.Close(); // Always close the stream regardless of what happens
   objStream=null;
   fso=null;
  }
 }
 catch(err){
  showMessage("Function loadFile() failed with parameters strAbsoluteFilePath=\"" + strAbsoluteFilePath + "\", strCharSet=\"" + strCharSet + "\". Message=\r\n" + err.description+"\r\nError Number: "+err.number);
 }
}
function saveFile(strAbsoluteFilePath, strCharSet, strFileContents, blnOverwrite){
 var adSaveCreateNotExist=1, adSaveCreateOverWrite=2, objStream = new ActiveXObject("ADODB.Stream"), fso=new ActiveXObject("Scripting.FileSystemObject");
 try{
  if(!strAbsoluteFilePath){
   throw new Error(1, "Required parameter \"strAbsoluteFilePath\" was not defined");
  }
  if(!strCharSet){
   throw new Error(2, "Required parameter \"strCharSet\" was not defined");
  }
  if(typeof strFileContents != "string"){
   throw new Error(3, "Required parameter \"strFileContents\" was not a string");
  }
  if(!fso.FolderExists(myFolderPath)){
   fso.CreateFolder(myFolderPath);
  }
  objStream.Open();
  try{
   objStream.CharSet=strCharSet;
   objStream.WriteText(strFileContents);
   objStream.SaveToFile(strAbsoluteFilePath, (blnOverwrite ? adSaveCreateOverWrite : adSaveCreateNotExist));
   return true;
  }
  catch(err){
   throw new Error(err.number, "SaveToFile failed:\r\n" + err.description);
  }
  finally{
   objStream.Close(); // Always close the stream regardless of what happens
   objStream=null;
   fso=null;
  }
  return false;
 }
 catch(err){
  showMessage("Function saveFile() failed with parameters strAbsoluteFilePath=\"" + strAbsoluteFilePath + "\", strCharSet=\"" + strCharSet + "\", strFileContents=\"" + strFileContents + "\", blnOverwrite=\"" + blnOverwrite + "\". Message=\r\n" + err.description+"\r\nError Number: "+err.number);
 }
}
function init(){
 loadButton.onclick=function(){loadFile(myFolderPath+"\myFile.txt","utf-8");};
 saveButton.onclick=function(){saveFile(myFolderPath+"\myFile.txt", "utf-8", gadgetContent.innerText, true);};
 closeButton.onclick=closeMessage;
}
</script>
</head>
<body onload="init()">
 <textarea id="gadgetContent">Bonjour</textarea>
 <div id="message">
  <div id="messageContent"></div>
  <div id="newButton"><button id="closeButton">Close</button></div>
 </div>
 <button id="loadButton">Load</button><button id="saveButton">Save</button>
</body>
</html>