When I run
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
myStore.CopyTextFile("HTDocs\\help.html", true);
I have error: "Operation not permitted on IsolatedStorageFileStream." It's from tutorial. How should I fix that?
public static class ISExtensions
{
public static void CopyTextFile(this IsolatedStorageFile isf, string filename, bool replace = false)
{
if (!isf.FileExists(filename) || replace == true)
{
StreamReader stream = new StreamReader(TitleContainer.OpenStream(filename));
IsolatedStorageFileStream outFile = isf.CreateFile(filename);
//
//
string fileAsString = stream.ReadToEnd();
byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(fileAsString);
outFile.Write(fileBytes, 0, fileBytes.Length);
stream.Close();
outFile.Close();
}
}
}
Try something like this:
// isolated storage
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
// access to resource files
StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri(#"HTDocs\help.html", UriKind.Relative));
using (Stream origFileStream = streamInfo.Stream)
{
// open the filestream for the isostorage file and copy the content from the original stream
using (IsolatedStorageFileStream fileStream = myStore.CreateFile("help.html"))
{
origFileStream.CopyTo(fileStream);
origFileStream.Close();
}
}
Related
I've create some sqlite database and place it in my project content , at this place
How can I check the first time user install the app and copy these 2 database to the Isolated Storage?
I've tried this way, but It gave me an error "Object reference not set to an instance of an object." at line
using (Stream stream = Application.GetResourceStream(new Uri("/Assets/DBS/MyPlaylist.db", UriKind.Relative)).Stream)
Here's my code
private static string DataFolder = "Datas";
public static void FirstTimeCopyDB()
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!isf.DirectoryExists(DataFolder))
{
isf.CreateDirectory(DataFolder);
using (Stream stream = Application.GetResourceStream(new Uri("/Assets/DBS/MyPlaylist.db", UriKind.Relative)).Stream)
{
using (IsolatedStorageFileStream isfs = isf.CreateFile("Datas/MyPlaylist.db"))
{
byte[] buffer = new byte[1024];
int byteRead = -1;
while ((byteRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
isfs.Write(buffer, 0, byteRead);
}
}
}
using (Stream stream = Application.GetResourceStream(new Uri("/Assets/DBS/MyDatabase.db",UriKind.Relative)).Stream)
{
using (IsolatedStorageFileStream isfs = isf.CreateFile("Datas/MyDatabase.db"))
{
byte[] buffer = new byte[1024];
int byteRead = -1;
while ((byteRead = stream.Read(buffer,0,buffer.Length))>0)
{
isfs.Write(buffer,0,byteRead);
}
}
}
}
else return;
}
}
Something like this:
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!store.FileExists("yourDBfileName"))
{
// copy yourDBfile
}
}
Explore more here
I wanted to save the unzipped file to a folder in IsolatedStorage. I have read the file zip file from IsolatedStorage and now want to unzip them into a folder. i have tried this way :-
private async Task UnZipFile(string fileName)
{
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(fileName, FileMode.Open, FileAccess.ReadWrite))
{
UnZipper unzip = new UnZipper(fileStream);
var filename = unzip.FileNamesInZip.FirstOrDefault();
if (filename != null)
{
// i can have the stream too. like this.
// var zipStream = unzip.GetFileStream(filename)
// here i am not getting how to save unzip file to a folder.
}
}
Here is what i got :) Hope it will help somebody.
private async Task UnZipFile()
{
// you can use Isolated storage too
var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
using (var fileStream = Application.GetResourceStream(new Uri("sample.zip", UriKind.Relative)).Stream)
{
var unzip = new UnZipper(fileStream);
foreach (string filename in unzip.FileNamesInZip)
{
if (!string.IsNullOrEmpty(filename))
{
if (filename.Any(m => m.Equals('/')))
{
myIsolatedStorage.CreateDirectory(filename.Substring(0, filename.LastIndexOfAny(new char[] { '/' })));
}
//save file entry to storage
using (var streamWriter =
new StreamWriter(new IsolatedStorageFileStream(filename,
FileMode.Create,
FileAccess.ReadWrite,
myIsolatedStorage)))
{
streamWriter.Write(unzip.GetFileStream(filename));
}
}
}
}
}
I want to be able to save images in IsolatedStorage, and later get it.
for this purpose I wrote a helper which I want to access it from everywhere in my app:
for creating an image:
public static void SaveImage(Stream image, string name)
{
try
{
IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (!isolatedStorage.DirectoryExists("MyImages"))
{
isolatedStorage.CreateDirectory("MyImages");
}
var filePath = Path.Combine("MyImages", name + ".jpg");
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Create, isolatedStorage))
{
StreamWriter sw = new StreamWriter(fileStream);
sw.Write(image);
sw.Close();
}
}
catch (Exception ex)
{
throw new Exception("failed");
}
}
and for getting that image again:
public static BitmapImage Get(string name)
{
try
{
IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
var filePath = Path.Combine("MyImages", name + ".jpg");
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Open, isolatedStorage))
{
BitmapImage image = new BitmapImage();
image.SetSource(fileStream);
return image;
}
}
catch (Exception ex)
{
throw new Exception("failed");
}
}
The problem appears when I try to set source of image I get this error message:
The component cannot be found. (Exception from HRESULT: 0x88982F50)
Assuming you have the right Capabilities under your WMAppManifest.xml what you need is:
public static void SaveImage(Stream image, string name)
{
var bitmap = new BitmapImage();
bitmap.SetSource(attachmentStream);
var wb = new WriteableBitmap(bitmap);
var temp = new MemoryStream();
wb.SaveJpeg(temp, wb.PixelWidth, wb.PixelHeight, 0, 50);
using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!myIsolatedStorage.DirectoryExists("MyImages"))
{
myIsolatedStorage.CreateDirectory("MyImages");
}
var filePath = Path.Combine("MyImages", name + ".jpg");
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Create, myIsolatedStorage))
{
fileStream.Write(((MemoryStream)temp).ToArray(), 0, ((MemoryStream)temp).ToArray().Length);
fileStream.Close();
}
}
}
That should work and store your image as a jpeg into your desired directory. if it doesn't, make sure that the create directory and Path.Combine() methods are being used correctly.
I'm trying to read a file entirely to a String variable.
I did this:
String text;
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var readStream = new IsolatedStorageFileStream("k.dat", FileMode.Open, store))
using (var reader = new StreamReader(readStream))
{
text= reader.ReadToEnd();
}
textBlock1.Text = text;`
It gave me a "Operation not permitted on IsolatedStorageFileStream" message from an IsolatedStorageException.
What am I doing wrong?
I tried by adding a .txt and .xml file in the file name, but it didn't work.
Where am I to put the file anyway? I tried
~\Visual Studio 2010\Projects\Parsing\Parsing\k.dat
I'm parsing it later using:
XmlReader reader = XmlReader.Create(new StringReader(xmldata));
flagLink = false;
while (reader.Read())
{
//and so on
Try with..
string text;
string filename="k.txt";
using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isolatedStorage.FileExists(fileName))
{
StreamReader reader = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.Open, isolatedStorage));
text = reader.ReadToEnd();
reader.Close();
}
if(!String.IsNullOrEmpty(text))
{
MessageBox.Show(text);
}
}
EDIT:
In case of xml,
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("test.xml", FileMode.Open); //you can use your filename just like above code
using (StreamReader reader = new StreamReader(isoFileStream))
{
this.textbox1.Text = reader.ReadToEnd();
}
}
}
catch
{ }
This is the entire method and this worked fully:
String sFile = "k.dat";
IsolatedStorageFile myFile = IsolatedStorageFile.GetUserStoreForApplication();
//myFile.DeleteFile(sFile);
if (!myFile.FileExists(sFile))
{
IsolatedStorageFileStream dataFile = myFile.CreateFile(sFile);
dataFile.Close();
}
var resource = Application.GetResourceStream(new Uri(#"k.dat", UriKind.Relative));
StreamReader streamReader = new StreamReader(resource.Stream);
string rawData = streamReader.ReadToEnd();
return rawData;
Im trying to get Images from URL and store it within my isolated storage, and then get the images from the isolated storage
here is my relevant code:
public void GetImages()
{
string uri = "http://sherutnetphpapi.cloudapp.net/mini_logos/" + path;
WebClient m_webClient = new WebClient();
imageUri = new Uri(uri);
m_webClient.OpenReadAsync(imageUri);
m_webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_ImageOpenReadCompleted);
m_webClient.AllowReadStreamBuffering = true;
}
void webClient_ImageOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
string iso_path = "~/SherutApp1;component/" + path;
var isolatedfile = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(iso_path, FileMode.Create, isolatedfile))
{
byte[] buffer = new byte[e.Result.Length];
while (e.Result.Read(buffer, 0, buffer.Length) > 0)
{
stream.Write(buffer, 0, buffer.Length);
}
}
}
i get the exception at the headline on this line:
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(iso_path, FileMode.Create, isolatedfile))
I don't know what is the problem, although i think that maybe images does not inserted correctly to the isolated storage.
I don't think you need the "~/SherutApp1;component/" part in there. If you the entire path is "test.jpg" or "folderThatExists\\test.jpg" it should work.