protected void PersistenceLoading(object sender, PersistenceLoadingEventArgs e)
{
string filePath = this.GetPersistenceFilePath(e.Page);
if (!File.Exists(filePath))
return;
// Persist the data in a file
using (FileStream fileStream = File.OpenRead(filePath))
{
e.PersistenceData = PersistenceData.FromStream(fileStream);
}
}
protected void PersistenceSaved(object sender, PersistenceEventArgs e)
{
if (e.PersistenceData == null)
{
return;
}
string filePath = this.GetPersistenceFilePath(e.Page);
if (File.Exists(filePath))
File.Delete(filePath);
using (FileStream fileStream = File.Create(filePath))
{
e.PersistenceData.ToStream(fileStream);
}
}
private string GetPersistenceFilePath(Page page)
{
string filePath = Server.MapPath("~/DirectoryToSave/");
string userName = (HttpContext.Current.User.Identity.IsAuthenticated)
? HttpContext.Current.User.Identity.Name
: this._userIPAdress;
string fileName = string.Format("{0}.{1}.xml",
userName.Replace('\\', '.'),
page.GetType().FullName);
return string.Format("{0}{1}", filePath, fileName);
}