簡體   English   中英

Windows小工具可以保存數據嗎?

[英]Can a Windows Gadget save data?

我正在創建一個小的Windows邊欄小工具,用於在簡單的textarea做筆記。

在此處輸入圖片說明

和往常一樣,我有一個gadget.xml清單文件和一個.html文件,請參見下文。

如何在Gadget中讀取一些數據/保存一些數據?

我知道通常僅使用JavaScript不可能localstorage這一點( 注意:因為我要持久化數據 ,所以無法使用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. System.Gadget.Settings對象的內置方法,可用於從Settings.ini文件(存儲在C:\\ Users \\ [user] \\ AppData \\ Local \\ Microsoft \\ Windows Sidebar中)讀取/寫入,但是如果小工具關閉或卸載,此信息將丟失。

  2. 使用FileSystemObject在任何地方創建或讀取或寫入文件夾/文件。 限制:文件只能保存為unicode或ascii。

  3. 使用ADO Stream對象可以在任何地方創建或讀取或寫入文件。 限制:無法創建文件夾-必須與FileSystemObject結合使用。 優點:可以使用計算機上存在的任何代碼頁。

由於我喜歡用utf-8保存文本文件,因此下面的示例使用第三種方法,但是您可以決定放棄一些錯誤處理。 (注意-此示例基於Andrew Urquhart發布腳本

幸運的是,側邊欄能夠使用已知的文件夾和已知的文件夾路徑,因此例如查找文檔文件夾的路徑就像

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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM