i know how to save a string into the IsolatedStorageFile but what about a byte[] ?
for a string i do:
string enc = "myString";
using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = iso.OpenFile("fileName", FileMode.Create))
{
var writer = new StreamWriter(stream);
writer.Write(enc);
writer.Close();
}
}
EDIT: okay i know now how to write, but how to read out the byte[] again?
Use BinaryWriter instead of StreamWriter.
To write a byte array, instead of enclosing your stream in a StreamWriter, use directly stream.Write. Or use a BinaryWriter.
For instance:
stream.Write(byteArray, 0, byteArray.Length);
Related
I am recording the user video and sending the data to the controller. The controller will receive the base64 data as a string. Then i am converting the base64 string to bytes like:
public ActionResult Content(string data)
{
byte[] ret = System.Text.Encoding.Unicode.GetBytes(data);
FileInfo fil = new FileInfo("D://test.mp4");
MemoryStream stream = new MemoryStream(ret);
var getdata = stream.GetBuffer();
using (Stream sw = fil.OpenWrite())
{
sw.Write(getdata, 0, getdata.Length);
sw.Close();
}
}
The video is downloading but the video is not playing the content. Can any body tell me what's the reason.
You need to recover the original byte array from the base64 string - use FromBase64String for that.
public ActionResult Content(string data)
{
byte[] ret = Convert.FromBase64String(data);
FileInfo fil = new FileInfo("D://test.mp4");
using (Stream sw = fil.OpenWrite())
{
sw.Write(ret , 0, ret .Length);
sw.Close();
}
}
What your code is doing is treating the base64 string as a unicode string, which it isn't.
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();
}
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.
I want to save text from a textbox to the internalStorage and load it from there...
The saving-part works fine. But the loading won't work I tried many tutorials already.
private void button2_Click(object sender, RoutedEventArgs e)
{
//get selected FileName from listBox
string selItem = listBox1.SelectedItem.ToString();
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (selItem != null)
{
IsolatedStorageFileStream fileStream = store.OpenFile(selItem, FileMode.Open, FileAccess.Read);
using (StreamReader sr = new StreamReader(fileStream))
{
String line = "";
//Debug.WriteLine("ReadLine");
if ((line = sr.ReadLine()) != null)
{
//Debug.WriteLine("ReadLineText");
textBox1.Text = line;
}
sr.Close();
}
fileStream.Close();
}
}
Instead of:
if ((line = sr.ReadLine()) != null)
{
//Debug.WriteLine("ReadLineText");
textBox1.Text = line;
I've tried many possibilities like: textBox1.Text = sr.ReadLine(); and so on..
The curious thing about he code is: If I enter for example:
IsolatedStorageFileStream fileStream = store.OpenFile("text0.txt", FileMode.Open, FileAccess.Read);
It works fine for the single file text0.txt.
Would be really really great if someone give me some tips to fix the code.
Thanks in advance..
this is how I open an ISF Stream
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.OpenOrCreate, isf); // loads from isolated storage
FYI: don't try to test without phone if you want to work with the isolated storage.
this finally works for me:
private void button2_Click(object sender, RoutedEventArgs e)
{
//get fileName
string filename = listBox1.SelectedItem.ToString();
try
{
IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.OpenOrCreate, store); // loads from isolated storage
//Debug.WriteLine(stream.CanRead);
StreamReader sr = new StreamReader(stream);
String lines = sr.ReadToEnd().ToString();
if (lines != null)
{
textBox1.Text = lines;
}
stream.Close();
sr.Close();
}
catch (Exception)
{
throw;
}
}
}
Maybe you see I killed the using(..) and put in a little check on "Null". I think the main cause was that there was no phone present to test the code.
Thank you very much indeed :-)))
I need to encrypt text/files in base 64 so I can send them in an email (I can't do attachments). I can use openSSL and GPG in Linux to encrypt and decrypt but don't know how to do the same in Windows XP. Does anyone know a program that can do this for me in windows?
EDITED AGAIN
In this link you can find how to encode/decode files.
I attach sample code:
private string FileToBase64(string srcFilename)
{
if (!string.IsNullOrEmpty(srcFilename))
{
FileStream fs = new FileStream(srcFilename,
FileMode.Open,
FileAccess.Read);
byte[] filebytes = new byte[fs.Length];
fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
string encodedData = Convert.ToBase64String(filebytes,
Base64FormattingOptions.InsertLineBreaks);
return encodedData;
}
}
private void Base64ToFile(string src, string dstFilename)
{
if (!string.IsNullOrEmpty(dstFilename))
{
byte[] filebytes = Convert.FromBase64String(src);
FileStream fs = new FileStream(dstFilename,
FileMode.CreateNew,
FileAccess.Write,
FileShare.None);
fs.Write(filebytes, 0, filebytes.Length);
fs.Close();
}
}