本文整理匯總了C#中Windows.Storage.StorageFolder.CreateTempFileAsync方法的典型用法代碼示例。如果您正苦於以下問題:C# StorageFolder.CreateTempFileAsync方法的具體用法?C# StorageFolder.CreateTempFileAsync怎麽用?C# StorageFolder.CreateTempFileAsync使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Windows.Storage.StorageFolder
的用法示例。
在下文中一共展示了StorageFolder.CreateTempFileAsync方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: SaveAsync
/// <summary>
/// Downloads a file from the specified address and returns the file.
/// </summary>
/// <param name="fileUri">The URI of the file.</param>
/// <param name="folder">The folder to save the file to.</param>
/// <param name="fileName">The file name to save the file as.</param>
/// <param name="option">
/// A value that indicates what to do
/// if the filename already exists in the current folder.
/// </param>
/// <remarks>
/// If no file name is given - the method will try to find
/// the suggested file name in the HTTP response
/// based on the Content-Disposition HTTP header.
/// </remarks>
/// <returns></returns>
public async static Task<StorageFile> SaveAsync(
Uri fileUri,
StorageFolder folder = null,
string fileName = null,
NameCollisionOption option = NameCollisionOption.GenerateUniqueName)
{
if (folder == null)
{
folder = ApplicationData.Current.LocalFolder;
}
var file = await folder.CreateTempFileAsync();
var downloader = new BackgroundDownloader();
var download = downloader.CreateDownload(
fileUri,
file);
var res = await download.StartAsync();
if (string.IsNullOrEmpty(fileName))
{
// Use temp file name by default
fileName = file.Name;
// Try to find a suggested file name in the http response headers
// and rename the temp file before returning if the name is found.
var info = res.GetResponseInformation();
if (info.Headers.ContainsKey("Content-Disposition"))
{
var cd = info.Headers["Content-Disposition"];
var regEx = new Regex("filename=\"(?<fileNameGroup>.+?)\"");
var match = regEx.Match(cd);
if (match.Success)
{
fileName = match.Groups["fileNameGroup"].Value;
await file.RenameAsync(fileName, option);
return file;
}
}
}
await file.RenameAsync(fileName, option);
return file;
}