Question

Using c#, I am downloading a file from a url the user enters on the phone. When it is writing the file to the IsolatedStorage, it is writing too many bytes to the file and therefore, the program used to open these files will not open.

When I debug, the bit size is 451,258 bytes, but when the file is exported from IsolatedStorage it is 454,656 bytes. It is filling the remaining space with spaces. Is there anyway to adjust this file size? Trim off the extra space at the end and save?

Forgive my ignorance as I am new at C# and WP7 developoment. I would really appreciate the help.

Here is my code :

       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);
        }

    }
Was it helpful?

Solution

Replace

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

with

var numberOfBytesRead = e.Result.Read(b, 0, b.Length);
isfs.Write(b, 0, numberOfBytesRead);
isfs.Flush();
isfs.Close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top