- Nov 3, 2011
- 48
- 0
- 0
Hi,
I am building an app which allows you to put in a url of a file and it downloads the file into IsolatedStorageFile. However, hopefully i have it in there, I would like to copy the file from there into a folder on my dropbox account.
I would really appreciate it if someone could show me how to copy the file or even just store the filename in a string (so i can at least see it is definitely storing the file)
Here is my code :
private void downloadButton_Click(object sender, RoutedEventArgs e)
{
webClient = new WebClient();
webClient.OpenReadCompleted += (s1, e1) =>
{
if (e1.Error == null)
{
try
{
string fileName = txtUrl.Text.Substring(txtUrl.Text.LastIndexOf("/") + 1).Trim();
bool isSpaceAvailable = IsSpaceIsAvailable(e1.Result.Length);
if (isSpaceAvailable)
{
// Save mp3 to Isolated Storage
using (var isfs = new IsolatedStorageFileStream(fileName,
FileMode.CreateNew,
IsolatedStorageFile.GetUserStoreForApplication()))
{
long fileLen = e1.Result.Length;
byte[] b = new byte[fileLen];
e1.Result.Read(b, 0, b.Length);
isfs.Write(b, 0, b.Length);
isfs.Flush();
}
}
else
{
MessageBox.Show("Not enough to save space available to download the file");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show(e1.Error.Message);
}
};
}
private bool IsSpaceIsAvailable(long spaceReq)
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
long spaceAvail = store.AvailableFreeSpace;
if (spaceReq > spaceAvail)
{
return false;
}
return true;
}
}
This is my first C# WP7 app and I have had very little experience with this - so I would really appreciate the help!
Thanks
SJG
I am building an app which allows you to put in a url of a file and it downloads the file into IsolatedStorageFile. However, hopefully i have it in there, I would like to copy the file from there into a folder on my dropbox account.
I would really appreciate it if someone could show me how to copy the file or even just store the filename in a string (so i can at least see it is definitely storing the file)
Here is my code :
private void downloadButton_Click(object sender, RoutedEventArgs e)
{
webClient = new WebClient();
webClient.OpenReadCompleted += (s1, e1) =>
{
if (e1.Error == null)
{
try
{
string fileName = txtUrl.Text.Substring(txtUrl.Text.LastIndexOf("/") + 1).Trim();
bool isSpaceAvailable = IsSpaceIsAvailable(e1.Result.Length);
if (isSpaceAvailable)
{
// Save mp3 to Isolated Storage
using (var isfs = new IsolatedStorageFileStream(fileName,
FileMode.CreateNew,
IsolatedStorageFile.GetUserStoreForApplication()))
{
long fileLen = e1.Result.Length;
byte[] b = new byte[fileLen];
e1.Result.Read(b, 0, b.Length);
isfs.Write(b, 0, b.Length);
isfs.Flush();
}
}
else
{
MessageBox.Show("Not enough to save space available to download the file");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show(e1.Error.Message);
}
};
}
private bool IsSpaceIsAvailable(long spaceReq)
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
long spaceAvail = store.AvailableFreeSpace;
if (spaceReq > spaceAvail)
{
return false;
}
return true;
}
}
This is my first C# WP7 app and I have had very little experience with this - so I would really appreciate the help!
Thanks
SJG