The code below catch the server path chosen by the user and saves on IsolatedStorage:
private void ConfSalvar(object sender, RoutedEventArgs e)
{
IsolatedStorageSettings iso = IsolatedStorageSettings.ApplicationSettings;
if (iso.Contains("isoServer"))
{
iso["isoServer"] = server.Text;
}
else
{
iso.Add("isoServer", server.Text);
}
}
The next code (another screen), uses the server path saved on IsolatedStorage for make an URL:
IsolatedStorageSettings iso = IsolatedStorageSettings.ApplicationSettings;
if (iso.TryGetValue<string>("isoServer", out retornaNome))
{
serv = retornaNome;
}
private void sinc(object sender, EventArgs e)
{
order.Visibility = Visibility.Collapsed;
client = new WebClient();
url = serv + "/json.html";
Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.OpenReadAsync(uri);
}
private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
string strFileName = url.Substring(url.LastIndexOf("/") + 1, (url.Length - url.LastIndexOf("/") - 1));
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
// Path Storage
// *** If File Exists
if (isoStore.FileExists(strFileName))
{
isoStore.DeleteFile(strFileName);
}
IsolatedStorageFileStream dataFile = new IsolatedStorageFileStream(strFileName, FileMode.CreateNew, isoStore);
long fileLen = e.Result.Length;
byte[] b = new byte[fileLen];
e.Result.Read(b, 0, b.Length);
dataFile.Write(b, 0, b.Length);
dataFile.Flush();
object lenghtOfFile = dataFile.Length;
dataFile.Close();
order.Visibility = Visibility.Visible;
ProgressBar1.Visibility = Visibility.Collapsed;
MessageBox.Show("Arquivo salvo!");
}
private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
ProgressBar1.Visibility = Visibility.Visible;
this.ProgressBar1.Value = e.ProgressPercentage;
}
So, if I saved the file path and immediately after click the button "sinc", an exception "An exception occurred during the operation, making the result invalid. Check InnerException for exception details.". But if I saved the file path and close the app, open the app and click the "sinc" button, it works.
Sorry for bad english
http://infassteste.url.ph
. If you store file path and quit the app, after come back and load it from "isolatedstorage", works fine. The problem happened when you save and immediately after load it. – Rene Sá