Domanda

con C #, sto scaricando un file da un URL l'utente immette sul telefono. Quando si sta scrivendo il file al IsolatedStorage, si sta scrivendo troppe byte per il file e quindi, il programma utilizzato per aprire questi file non si apre.

Quando il debug, la dimensione bit è 451,258 byte, ma quando il file viene esportato da IsolatedStorage è 454,656 byte. Si sta riempiendo lo spazio rimanente con spazi. Esiste un modo per regolare questa dimensione del file? Tagliare lo spazio extra alla fine e salvare?

Perdona la mia ignoranza come io sono nuovo in C # e WP7 developoment. Vorrei davvero apprezzare l'aiuto.

Ecco il mio codice:

       public void readCompleteCallback(Object sender, OpenReadCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            try
            {
                //string fileName = txtUrl.Text.Substring(txtUrl.Text.LastIndexOf("/") + 1).Trim();
                string fileName = "DownloadedNZB.nzb";
                bool isSpaceAvailable = IsSpaceIsAvailable(e.Result.Length);

                if (isSpaceAvailable)
                {
                    // Save mp3 to Isolated Storage
                    using (var isfs = new IsolatedStorageFileStream(fileName,
                                        FileMode.CreateNew,
                                        IsolatedStorageFile.GetUserStoreForApplication()))
                    {
                        long fileLen = e.Result.Length;
                        byte[] b = new byte[fileLen];
                        e.Result.Read(b, 0, b.Length);
                        isfs.Write(b, 0, b.Length);
                        isfs.Flush();
                        isfs.Close();
                        MessageBox.Show("File downloaded successfully");                      
                    }

                }
                else
                {
                    MessageBox.Show("Not enough to save space available to download the file");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        else
        {
            MessageBox.Show(e.Error.Message);
        }

    }
È stato utile?

Soluzione

Sostituire

e.Result.Read(b, 0, b.Length);
isfs.Write(b, 0, b.Length);
isfs.Flush();
isfs.Close();

con

var numberOfBytesRead = e.Result.Read(b, 0, b.Length);
isfs.Write(b, 0, numberOfBytesRead);
isfs.Flush();
isfs.Close();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top