我使用C#和Windows Phone 8.1作为通用。
我遇到BackgroundDownloader问题。当我开始下载时,它给了我一个例外:
Downloading http://localhost/MySong.mp3 to MySong.mp3 with Default priority, a95c00db-738d-4e22-a456-dc30d49b0a3b
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
Exception: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
这是我的代码:
private void downladButton_Tapped(object sender, TappedRoutedEventArgs e)
{
PumpUrl(txtUrl.Text);
}
async void PumpUrl(string url)
{
if (!string.IsNullOrEmpty(url) && CheckUrl(url))
StartDownloadMethod(url);
else
await new MessageDialog("Looks like URL is invalid.\r\nPlease check your URL and try again"").ShowAsync();
}
public bool CheckUrl(string URL)
{
Uri source;
if (Uri.TryCreate(URL, UriKind.RelativeOrAbsolute, out source))
return true;
else
return false;
}
public void StartDownloadMethod(string url, string fileName = null)
{
StartDownload(url, BackgroundTransferPriority.Default, false, fileName);
}
private async void StartDownload(string url, BackgroundTransferPriority priority, bool requestUnconstrainedDownload, string fileName = null)
{
Uri source;
if (!Uri.TryCreate(url.Trim(), UriKind.RelativeOrAbsolute, out source))
{
Debug.WriteLine("Invalid URI.");
return;
}
string destination = null;
if (string.IsNullOrEmpty(fileName))
try
{
destination = System.IO.Path.GetFileName(Uri.UnescapeDataString(url)).Trim();
}
catch { }
else
destination = Uri.UnescapeDataString(fileName).Trim();
if (string.IsNullOrWhiteSpace(destination))
{
Debug.WriteLine("A local file name is required.");
return;
}
StorageFile destinationFile = null;
try
{
if (vars.localType == LocalType.SDCard)
destinationFile = await KnownFolders.RemovableDevices.CreateFileAsync(
"Downloads\\Test App\\" + destination, CreationCollisionOption.GenerateUniqueName);
else
destinationFile = await KnownFolders.MusicLibrary.CreateFileAsync(
"Test App\\" + destination, CreationCollisionOption.GenerateUniqueName);
}
catch (FileNotFoundException ex)
{
Debug.WriteLine("Error while creating file: " + ex.Message);
return;
}
catch (Exception ex) { Debug.WriteLine("Error while creating file: " + ex.Message); }
if (destinationFile == null)
return;
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source, destinationFile);
Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Downloading {0} to {1} with {2} priority, {3}",
source.AbsoluteUri, destinationFile.Name, priority, download.Guid));
download.Priority = priority;
Add(url, destination, download);
if (!requestUnconstrainedDownload)
{
// Attach progress and completion handlers.
await HandleDownloadAsync(download, true);
return;
}
List<DownloadOperation> requestOperations = new List<DownloadOperation>();
requestOperations.Add(download);
UnconstrainedTransferRequestResult result;
try
{
result = await BackgroundDownloader.RequestUnconstrainedDownloadsAsync(requestOperations);
}
catch (NotImplementedException)
{
Debug.WriteLine(
"BackgroundDownloader.RequestUnconstrainedDownloadsAsync is not supported in Windows Phone.");
return;
}
Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Request for unconstrained downloads has been {0}",
(result.IsUnconstrained ? "granted" : "denied")));
await HandleDownloadAsync(download, true);
}
private async Task HandleDownloadAsync(DownloadOperation download, bool start)
{
try
{
Debug.WriteLine("Running: " + download.Guid);
bool bb = false;
foreach (DownloadOperation item in DownloadDB.activeDownloads)
{
if (item != null && item.Guid == download.Guid)
{
bb = true;
break;
}
}
if (!bb)
DownloadDB.activeDownloads.Add(download);
Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress);
CancellationTokenSource cts = new CancellationTokenSource();
AppendCancellationTokenSource(download, cts);
if (start)
await download.StartAsync().AsTask(cts.Token, progressCallback);
else
await download.AttachAsync().AsTask(cts.Token, progressCallback);
ResponseInformation response = download.GetResponseInformation();
DownloadDB.RemoveChildByGuid(download.Guid.ToString());
DownloadDB.SaveDB();
Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Completed: {0}, Status Code: {1}",
download.Guid, response.StatusCode));
}
catch (TaskCanceledException taskCanceled)
{
Debug.WriteLine("TaskCanceledException: " + download.Guid + "\t" + taskCanceled.Message);
DownloadDB.RemoveChildByGuid(download.Guid.ToString());
}
catch (Exception ex)
{
Debug.WriteLine("Exception: " + ex.Message);
}
finally
{
DownloadDB.RemoveChildByGuid(download.Guid.ToString());
DownloadDB.activeDownloads.Remove(download);
}
}
我在3个月前在其他应用程序中使用了我的代码,它工作正常,但在此应用程序中工作。哪里错了?
由于