Personal Tier applications can use the Isolated Storage feature supplied with the NGWS runtime. Isolated storage is key, since it is a local store, but using it does not allow the application to access the file system.
The following example demonstrates how you can store and retrieve data from Isolated Storage for a Personal Tier application.
<%@ Page ClientTarget="downlevel" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.IO.IsolatedStorage" %> <%@ Import Namespace="System.Runtime.Serialization.Formatters.Binary" %> <html> <head> <script language="C#" runat="server"> String _storeFile = "mystore"; void Page_Load(Object sender, EventArgs e) { ArrayList values = RecallLastValues(); if( null != values ) { // field order: principal, interest rate, months Principal.Text = values[0].ToString(); Rate.Text = values[1].ToString(); Months.Text = values[2].ToString(); } } ArrayList RecallLastValues() { ArrayList values = new ArrayList(); // read the last values if possible from IsolatedStorage // they're stored as a serialized array // in order: principal, interest rate, months IsolatedStorageFileStream stream; stream = new IsolatedStorageFileStream(_storeFile, FileMode.Open); if( null != stream ) { values = (ArrayList)BinarySerializer.Deserialize(stream); } else { // just add three default values values.Add(100000.0); values.Add(8.5); values.Add(360); } return values; } void RememberLastValues(double principal, double interestRate, int months) { // write the last values out to IsolatedStorage // just serialize them as a three element array IsolatedStorageFileStream stream; stream = new IsolatedStorageFileStream(_storeFile, FileMode.OpenOrCreate); ArrayList values = new ArrayList(); values.Add(principal); values.Add(interestRate); values.Add(months); BinarySerializer.Serialize(values, stream); stream.Close(); } void Calculate_OnClick(Object source, EventArgs e) { if( !Page.IsValid ) { Msg.Text = "Please enter all required values"; return; } double rateAmount = Double.Parse(Rate.Text); double principalAmount = Double.Parse(Principal.Text); int monthCount = Int32.Parse(Months.Text); double payment = Calculate(principalAmount, rateAmount, monthCount); Msg.Text = "Approximate payment is " + Double.Format(payment, "C") + " monthly"; RememberLastValues(principalAmount, rateAmount, monthCount); } double Calculate(double principal, double interestRate, int months) { // account for interest rates like 9.0, which we'll assume is 9% // and 0.9, which we'll also assume is 9% if( interestRate >= 1.0 ) interestRate /= 100.0; double rate = (interestRate / 12) + 1.0; double top = principal * System.Math.Pow(rate, (double)months); double bottom = 0; for( int m = months - 1; m > 0; m-- ) { bottom += System.Math.Pow(rate, (double)m); } return top / bottom; } </script> </head> <body> <h4>My Web Mortage Calculator</h4> <form runat="server"> <table style="background-color:#ccccff;"> <tr> <td>Principal:</td> <td><asp:TextBox id="Principal" runat="server"/></td> <td><ASP:RequiredFieldValidator ControlToValidate="Principal" Display="Static" ErrorMessage="*" runat="server"/> </td> </tr> <tr> <td>Interest Rate:</td> <td><asp:TextBox id="Rate" runat="server"/></td> <td><ASP:RequiredFieldValidator ControlToValidate="Rate" Display="Static" ErrorMessage="*" runat="server"/> </td> </tr> <tr> <td>Months</td> <td><asp:TextBox id="Months" runat="server"/></td> <td><ASP:RequiredFieldValidator ControlToValidate="Months" Display="Static" ErrorMessage="*" runat="server"/> </td> </tr> </table> <br> <asp:Button OnClick="Calculate_OnClick" Text="Calculate" runat="server"/> <br> <asp:Label id="Msg" runat="server"/> </form> </body> </html>
See Also