0
votes

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

1
can you tell me where in the code the Exception is thrown? Is it when clicking Sinc button? Coz i replicated you code only the adding and retrieving, it works fine. BTW, im sorry my previous answer was incorrect.Kasun Kodagoda
when i clicked the "sinc" button is thrown an exception.Rene Sá
Did u check to see if the value from IsolatedStorageSettings is loaded properly? Can u provide the value u store as the server path so i can check the rest of your code?Kasun Kodagoda
You can use 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á
Its done. :) Check the answer.Kasun Kodagoda

1 Answers

1
votes

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 :)