storing a project folder in isolated storage - visual-studio-2010

I am creating a windows phone project with static html files in a folder called "webapplication". i want to store all contents of "webapplication" folder in the isolated storage. Can some one help to resolve this?

Check out windows phone geek at http://windowsphonegeek.com/tips/all-about-wp7-isolated-storage-files-and-folders for information on Isolated Storage.
To create a folder do the following:
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
myIsolatedStorage.CreateDirectory("NewFolder");
If you want to create a file inside the folder then:
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("NewFolder\\SomeFile.txt", FileMode.CreateNew, myIsolatedStorage));
If you are looking to copy the files to IsolatedStorage, then you need to run the following code the 1st time your application executes:
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
string[] files = System.IO.Directory.GetFiles(folderPath);
foreach (var _fileName in files)
{
if (!storage.FileExists(_fileName))
{
string _filePath = _fileName;
StreamResourceInfo resource = Application.GetResourceStream(new Uri(_filePath, UriKind.Relative));
using (IsolatedStorageFileStream file = storage.CreateFile("NewFolder\\SomeFile.txt", FileMode.CreateNew, Storage))
{
int chunkSize = 102400;
byte[] bytes = new byte[chunkSize];
int byteCount;
while ((byteCount = resource.Stream.Read(bytes, 0, chunkSize)) > 0)
{
file.Write(bytes, 0, byteCount);
}
}
}
}
}

Related

Overwrite filename in SetAttributesCallback in Azure Storage Data Movement Library

I need to lowercase all filenames during UploadDirectoryAsync - is this possible to control or set via 'SetAttributesCallback'??
I cannot control the local physical files or rename them locally before uploading them to azure via Azure Storage Data Movement Library.
The end result will be that source and destination filename always will be with lowercase.
Any solution out there??
I need to lowercase all filenames during UploadDirectoryAsync - is this possible to control or set via 'SetAttributesCallback'
Yes, we could do that in the SetAttributesCallback, currently there is no rename Azure blob API, so we could upload the required renamed file in the SetAttributesCallback and delete the UploadDirectoryAsync load file. I also test it on my side, it works correctly.
The following is my demo code.
using System;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.DataMovement;
namespace DataMovementTest
{
class Program
{
static void Main(string[] args)
{
string storageConnectionString = "storage connection string";
CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference("container name");
blobContainer.CreateIfNotExists();
var destDir = blobContainer.GetDirectoryReference("directory name");
string sourceDirPath = #"local path";
var options = new UploadDirectoryOptions
{
Recursive = false,
BlobType = BlobType.BlockBlob
};
using (MemoryStream journalStream = new MemoryStream())
{
// Store the transfer context in a streamed journal.
DirectoryTransferContext context = new DirectoryTransferContext(journalStream)
{
SetAttributesCallback = (destination) =>
{
CloudBlob destBlob = destination as CloudBlob;
if (System.Text.RegularExpressions.Regex.IsMatch(destBlob.Uri.Segments.Last(), "[A-Z]")) //check whether blobName contains uppercase
{
var path = sourceDirPath + $"/{destBlob.Uri.Segments.Last()}";
Console.WriteLine(path);
var renameBlob = destDir.GetBlockBlobReference(destBlob.Uri.Segments.Last().ToLower());
using (var fileStream = File.OpenRead(path))
{
renameBlob.UploadFromStream(fileStream);
}
destBlob.DeleteIfExists();
}
},
ShouldTransferCallback = (source, destination) => true
};
CancellationTokenSource cancellationSource = new CancellationTokenSource();
try
{
// Start the upload
var uploadResult = TransferManager.UploadDirectoryAsync(sourceDirPath, destDir, options, context, cancellationSource.Token).Result;
}
catch (Exception e)
{
Console.WriteLine("The transfer is cancelled: {0}", e.Message);
}
Console.WriteLine("Files in directory {0} uploading to {1} is finished.", sourceDirPath, destDir.Uri.ToString());
}
}
}
}

How to use DotNetZip Library

I unziped the file I donwloaded. how and what library do i access in my asp.net mvc 3 app to be able to unzip a file that contains multiple files?
my controller code:
public ActionResult Upload(ScormUploadViewModel model)
{
if (ModelState.IsValid)
{
if (model.ScormPackageFile != null)
{
string zipCurFile = model.ScormPackageFile.FileName;
string fullPath = Path.GetDirectoryName(zipCurFile);
string directoryName = Path.GetFileNameWithoutExtension(zipCurFile);
Directory.CreateDirectory(Path.Combine(fullPath, directoryName));
using (FileStream zipFile = new FileStream(zipCurFile, FileMode.Open))
{
using (GZipStream zipStream = new GZipStream(zipFile, CompressionMode.Decompress))
{
StreamReader reader = new StreamReader(zipStream);
//next unzip the file? how to get the library i need?
}
}
thanks

how to save a variable include image into isolate storage in windowsphone

i've been doing a essay about windowsphone. i created a address variable include a uri to add a image into address. There is a error when i use Isolate storage to save data. I don't know why.
Please help me!
Thank you so much.
class Address
{
private string name;
private Uri icon;
.....
}
......
public void save()
{
XmlWriterSettings xmlwritersetting = new XmlWriterSettings();
xmlwritersetting.Indent = true;
using (IsolatedStorageFile myisolatedstiragefile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myisolatedstiragefile.FileExists(filename))
{
myisolatedstiragefile.DeleteFile(filename);
}
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, System.IO.FileMode.OpenOrCreate, myisolatedstiragefile))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Adress>));
using (XmlWriter writer = XmlWriter.Create(stream, xmlwritersetting))
{
serializer.Serialize(writer, listadress);
}
}
}
}
It's a little difficult for me to understand your question, but I'll try. You really should indicate what error you specifically get in the debugger and where it occurs.
But just by looking, it seems that you might be trying to use the XmlSerializer to write binary image data to iso-storage and that probably won't work. You can find many examples of using iso-storage for various purposes including writing image files here:
http://www.windowsphonegeek.com/tips/All-about-WP7-Isolated-Storage---Read-and-Save-Images
For example, it shows that you can save a JPG image to isolated storage by doing this:
// Create a filename for JPEG file in isolated storage.
String tempJPEG = "logo.jpg";
// Create virtual store and file stream. Check for duplicate tempJPEG files.
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) {
if (myIsolatedStorage.FileExists(tempJPEG)) {
myIsolatedStorage.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);
StreamResourceInfo sri = null;
Uri uri = new Uri(tempJPEG, UriKind.Relative);
sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode WriteableBitmap object to a JPEG stream.
Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
//wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85); fileStream.Close();
}

Where is temporary folder in IsolatedStorage (Windows Phone)?

I can not find where is temporary folder to add temp file into.
How do i find?
You just create your own folder and manage the content:
private void SaveTempFile(string fileName, object data)
{
var storage = IsolatedStorageFile.GetUserStoreForApplication();
if (storage.DirectoryExists("temp") == false)
storage.CreateDirectory("temp");
fileName = Path.Combine("temp", fileName);
using (var fileStream = new IsolatedStorageFileStream(fileName, FileMode.Create, storage))
{
//Write the data
using (var isoFileWriter = new StreamWriter(fileStream))
{
// write your data in the format of your choice
}
}
}
Delete the file whenever you want to
public void DeleteTempFile(string fileName)
{
try
{
var storage = IsolatedStorageFile.GetUserStoreForApplication();
if (storage.DirectoryExists("temp") == false) return;
fileName = Path.Combine("temp", fileName);
if (storage.FileExists(fileName))
{
storage.DeleteFile(fileName);
}
}
catch (Exception) { }
}
There is no that kind of folder prepared for app on Windows Phone. You have to create it on your own and manage clearing the content form there when it's no longer needed. However you don't need to bother about deleting that files when your app is uninstalled - whole application folder is deleted from isolated storage then.

How to save data in xml file in windows phone 7

Hello Everyone,
I am working on an application in which i need to save some data in IsolatedStorage .
while my application is running i am able to see data from file. Once i close my application and restarting my application its not showing my data.
public static IsolatedStorageFile isstore = IsolatedStorageFile.GetUserStoreForApplication();
public static IsolatedStorageFileStream xyzStrorageFileStream = new IsolatedStorageFileStream("/category.xml", System.IO.FileMode.OpenOrCreate, isstore);
public static XDocument xmldoc = XDocument.Load("category.xml");
favouriteDoc.Save(rssFavouriteFileStream);
rssFavouriteFileStream.Flush();
Any one having any idea? How to do this?
In order to save structured data you need to use XML Writer or XML Serializer.
For example to save data:
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("People2.xml", FileMode.Create, myIsolatedStorage))
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(isoStream, settings))
{
writer.WriteStartElement("p", "person", "urn:person");
writer.WriteStartElement("FirstName", "");
writer.WriteString("Kate");
writer.WriteEndElement();
writer.WriteStartElement("LastName", "");
writer.WriteString("Brown");
writer.WriteEndElement();
writer.WriteStartElement("Age", "");
writer.WriteString("25");
writer.WriteEndElement();
// Ends the document
writer.WriteEndDocument();
// Write the XML to the file.
writer.Flush();
}
}
}
To read it back:
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("People2.xml", FileMode.Open);
using (StreamReader reader = new StreamReader(isoFileStream))
{
this.tbx.Text = reader.ReadToEnd();
}
}
}
catch
{ }
Answer is taken from this article, so all the credits go to WindowsPhoneGeek. Also, see other examples in the aforementioned article header.

Resources