Windows Gadget可以保存数据吗?

时间:2015-01-05 16:45:58

标签: windows-desktop-gadgets

我正在创建一个小的Windows边栏小工具,用于在简单的textarea中记笔记。

enter image description here

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

如何在小工具中读取一些数据/保存一些数据?

我知道这通常不可能只使用JavaScript(注意:使用localstorage是不可能的,因为我需要持久的数据),所以如何保存/读取里面的数据一个Windows Gadget


<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 个答案:

答案 0 :(得分:1)

尝试以下方法之一:

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

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

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

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

    幸运的是,侧边栏可以使用knownfoldersknownfolderpaths,因此查找文档文件夹的路径就像

    一样简单
    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>