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
This had nothing to do with the IsolatedStorageSettings
. It works fine. The problem was when you were creating the Uri
you set the UriKind
as RelativeOrAbsolute
.
Thats what threw the Exception and InnerException states that This operations is not supported for a relative URI
. What you need to do is change the UriKind
to Absolute
. So the code block should look like this.
private void sinc(object sender, EventArgs e)
{
if (iso.TryGetValue<string>("isoServer", out retornaNome))
{
serv = retornaNome;
}
else
{
// Let the user know.
return;
}
order.Visibility = Visibility.Collapsed;
client = new WebClient();
url = serv + "/json.html";
Uri uri = new Uri(url, UriKind.Absolute); // <<< Absolute instead of RelativeOrAbsolute
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.OpenReadAsync(uri);
}
That should fix your problem and work just fine :)