How to store DateTime in isolated storage in Windows Phone? - windows-phone-7

I developing an application for Windows Phone. And, here I have a requirement to store DateTime variable in isolated storage.
Is this possible? I know basic types like strings and ints etc can be stored.
Kindly help me thru this

You could store the value as Ticks in isolated storage and then when you read it just initialize a new DateTime instance from it. Ticks is defined as long so it should be straight forward to store it.
var valueToStore = DateTime.Now.Ticks;
// Save to isolated storage
var storedValue = ReadFromIsolatedStorage();
var dateTime = new DateTime(storedValue);

You can also use
DateTime.Parse(string);
Convert to string and save in isolated storage. And Retrieve using above statement from storage.

Related

Last Access Time of the Azure storage files

Is there way to determine Last Access Time of the Azure storage files apart from log analytics . So, does anyone ever come across this situation, what would be the best way to achieve this? Or am I too concerned about this?
Thank you in advanced.
In Azure file storage, there is no option till date to know the last opened/viewed/accessed time, but there is a possibility to get to know about the last modified file. In case you are looking for that, here's a C# way:
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse("<Your Connection string>");
CloudFileClient cloudFileClient = cloudStorageAccount.CreateCloudFileClient();
CloudFileShare cloudFileShare = cloudFileClient.GetShareReference("<Your File Share Name>");
IEnumerable<IListFileItem> fileShareItemsList = cloudFileShare.GetRootDirectoryReference().ListFilesAndDirectories();
foreach (IListFileItem listItem in fileShareItemsList)
{
if (listItem is CloudFile) // Checking direct files under the root directory for now
{
CloudFile file = (CloudFile)listItem;
file.FetchAttributes(); // this is mandatory to fetch modified time
DateTime lastModifiedTime = file.Properties.LastModified; // Here's the time!
// Use it in your logic..
}
}

Windows Azure Stored Access Policy Remove

I want to download blobs using Shared access signatures, SAS.
I also want to be able to remove active SAS URI's and, if I understand it correctly, I must use Stored Access Policy for this.
What confuses me is how I can remove a policy. I also read you can only have 5 stored access policies active?
My goal here is to be able to remove an active SAS URI. The only way I can think of accomplishing this is to remove the policy that the SAS URI is linked with, right? If I have over hundreds of files in my blob storage, how in the world can I make this work? I can't have one policy for each blob right? 5 is the maximum policies?
This code demonstrates how I add a policy and how I create a SAS URI that uses this policy, which users can download from.
static void CreateSharedAccessPolicy(CloudBlobContainer container)
{
//Create a new stored access policy and define its constraints.
SharedAccessBlobPolicy sharedPolicy = new SharedAccessBlobPolicy()
{
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(10),
Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List
};
//Get the container's existing permissions.
BlobContainerPermissions permissions = new BlobContainerPermissions();
//Add the new policy to the container's permissions.
permissions.SharedAccessPolicies.Clear();
permissions.SharedAccessPolicies.Add("PolicyName", sharedPolicy);
container.SetPermissions(permissions);
}
static string GetBlobSasUriWithPolicy(CloudBlobContainer container, string policyName)
{
//Get a reference to a blob within the container.
CloudBlockBlob blob = container.GetBlockBlobReference("file_name");
//Generate the shared access signature on the blob.
string sasBlobToken = blob.GetSharedAccessSignature(null, "PolicyName");
//Return the URI string for the container, including the SAS token.
return blob.Uri + sasBlobToken;
}
One last question, how do I remove a policy? Is it as simple as:
permissions.SharedAccessPolicies.Remove("PolicyName");
My goal here is to be able to remove an active SAS URI. The only way I
can think of accomplishing this is to remove the policy that the SAS
URI is linked with, right?
Partly correct. Removing the access policy is one way to do it. Other would be to change the name of the policy (policy identifier). For example if the policy identifier is mypolicy then changing it to mypolicy1 would have the same effect as removing the policy.
If I have over hundreds of files in my blob storage, how in the world
can I make this work?
As you may already know, access policy is defined at the blob container level and not at the blob level. Removing/invalidating an access policy would make invalidate SAS URL for all blobs in that container.
I can't have one policy for each blob right? 5 is the maximum
policies?
That is correct.
One last question, how do I remove a policy? Is it as simple as:
permissions.SharedAccessPolicies.Remove("PolicyName");
That is correct. Make sure you save it back though. You can use something like:
var cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;
var blobClient = cloudStorageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("container-name");
var containerPermissions = container.GetPermissions();
containerPermissions.SharedAccessPolicies.Remove("access-policy-id");
container.SetPermissions(containerPermissions);

asp.net MVC memory stream DICOM image to session variable

I am generating images on fly from DICOM files using:
public ActionResult Generatemage()
{
FileContentResult data;
.....
objImage = im.Bitmap(outputSize, PixelFormat.Format24bppRgb, m);
using (var memStream = new MemoryStream())
{
objImage.Save(memStream, ImageFormat.Png);
data = this.File(memStream.GetBuffer(), "image/png");
}
return data;
}
Can I store the image as a session variable so I can modify it using Point3D?
I tried to use:
Bitmap data = (Bitmap)Session["newimage"];
Got these two errors:
Cannot implicitly convert type 'System.Drawing.Bitmap' to 'System.Web.Mvc.FileContentResult' and
A local variable named 'data' is already defined in this scope
I would appreciate your suggestions, thanks in advance.
Can I store the image as a session variable so I can modify it using
Point3D?
I suggest to not do that. If you have not read Nathanael's post on image resizing pitfalls then I suggest you do so now. It may be talking about resizing but it also give hints on working with images in general. On point #3 it says:
Serving a file from disk by loading it into memory. Think about how
much RAM your server has, how large a single image is, how long it has
to stay in memory before users fi downloading it, and how many
users you have requesting images.
In your particular case you can replace "before users finish downloading it" with "before Point3D finish processing the image". So, what I suggest is that you get a handle to that file, say maybe there's an Id that uniquely identifies a file per user, use that Id to retrieve the file when it's time to process it with Point3D, load it into a MemoryStream (assuming Point3D can work with mem. stream), process it, then dispose of it. In that manner you are only holding on to the image for the duration of "Point3D processing".
Cannot implicitly convert type 'System.Drawing.Bitmap' to
'System.Web.Mvc.FileContentResult' and A local variable named 'data'
is already defined in this scope
That is most probably because you have defined data as such:
FileContentResult data;
and then you are doing a:
Bitmap data = (Bitmap)Session["newimage"];
same variable of two different types within the same scope.

how to save and load a object in a windows phone 7 app?

is it possible in a WP7 app to save some objects which i create and then load it when the app is started again?
You'll want to look to store persistent items into IsolatedStorage. You can see an overview and an example of how to use IsolatedStorage here. There are also a range of examples on this site, showing how to save different types of objects.
Here's an example storing a string, but you should be able to store any type of object this way.
Add IsolatedStorage to your references:
using System.IO.IsolatedStorage;
In your class:
private string myString;
In the Loaded event for your page:
try
{
myString = (string)IsolatedStorageSettings.ApplicationSettings["myString"];
}
catch
{
IsolatedStorageSettings.ApplicationSettings.Add("myString", "this value is a string");
}
and later, when you want to save:
IsolatedStorageSettings.ApplicationSettings["myString"] = myString;
try after
the example code above to add this.
IsolatedStorageSettings.ApplicationSettings.save

how to manage the data in an xml file in wp7?

I have written a simple wp7 application. i am using wcf service to interact with the database. Now i want to store a part of user's info in the mobile also. this info needs to be accessible across the wp7 app.
I found multiple ways to do this like : isolated storage, resource files or static data in the app.xaml
Which one would be more suitable? as i may wish to edit the data in future...i may not opt for packaged files as they are read-only. also do not wish to lose data by storing in isolated storage.
Please suggest the most suitable option for me
Thanks in advance
Bindu
It sounds like you want to store downloaded data between uses of the app. In this case Isolated Storage is probably your best bet. It will remain in the phone's non-volatile memory and you will not lose it.
Details here
Resource files and static data in the app.xaml won't work for you since you want to be able to change these items at a later date since these will be read only.
I don't know what you are referring to when you say "lose data" by storing in IsolatedStorage. This is your best bet and is actually really easy to do. Here is an example of saving a simple boolean:
private void SaveSettings()
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["VibrationOn"] = VibrationOn;
}
Then to load it later:
private void LoadSettings()
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
bool vo;
if (settings.TryGetValue<bool>("VibrationOn", out vo))
VibrationOn = vo;
else
VibrationOn = true;
}
You would call your LoadSettings() method in the Application_Launching and Application_Activated events and then your SaveSettings() in the Application_Deactivated and Application_Closing events within your App.xaml.cs.
You can also serialize objects or write whole files.

Resources