Windows Phone - 如何从Web服务器下载多个文件并保存在IsolatedStorage上?

时间:2014-08-14 17:16:02

标签: c# windows-phone-8 isolatedstorage

我的应用程序从Web服务器下载JSON文件(json.html)并将文件保存在IsolatedStorage上。这段代码工作正常。

所以,我需要从同一个Web服务器下载,但需要更多文件(“json.html”,“json1.html”和“json2.html”)并保存在IsolatedStore上。

如何使代码适应更多文件?

该文件必须与Web服务器中的文件同名保存(如我的代码所示)。

我的代码:

 private void sinc(object sender, EventArgs e)
    {
        IsolatedStorageSettings iso = IsolatedStorageSettings.ApplicationSettings;



        if (iso.TryGetValue<string>("isoServer", out retornaNome))
        {
            serv = retornaNome;
        }

        else {
            MessageBox.Show("Configure um servidor antes de sincronizar os dados!");
            return;

        }

            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;

    }

0 个答案:

没有答案