当前位置: 首页>>代码示例>>C#>>正文


C# StorageFolder.CreateTempFileAsync方法代码示例

本文整理汇总了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;
        }
开发者ID:siatwangmin,项目名称:WinRTXamlToolkit,代码行数:62,代码来源:WebFile.cs


注:本文中的Windows.Storage.StorageFolder.CreateTempFileAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。