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


C# IsolatedStorageFileStream.WriteAsync方法代码示例

本文整理汇总了C#中System.IO.IsolatedStorage.IsolatedStorageFileStream.WriteAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IsolatedStorageFileStream.WriteAsync方法的具体用法?C# IsolatedStorageFileStream.WriteAsync怎么用?C# IsolatedStorageFileStream.WriteAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.IsolatedStorage.IsolatedStorageFileStream的用法示例。


在下文中一共展示了IsolatedStorageFileStream.WriteAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetFile

        public async Task<string> GetFile(FileLocation fileLocation) {
            logger.debug("Getting file {0}", fileLocation);
            if(fileLocation.Constructor == Constructor.fileLocation) {
                FileLocationConstructor location = (FileLocationConstructor) fileLocation;
                string filePath = FileLocationToCachePath(location);
                using(IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) {
                    if(!storage.DirectoryExists("cache")) {
                        storage.CreateDirectory("cache");
                    }

                    if(storage.FileExists(filePath)) {
                        logger.debug("Getting file {0} from cache", filePath);
                        return filePath;
                    }

                    TLApi api = await session.GetFileSession(location.dc_id);
                    logger.debug("Got file session for dc {0}", location.dc_id);

                    Upload_fileConstructor file = (Upload_fileConstructor) await api.upload_getFile(TL.inputFileLocation(location.volume_id, location.local_id, location.secret), 0, int.MaxValue);

                    logger.debug("File constructor found");

                    using (Stream fileStream = new IsolatedStorageFileStream(filePath, FileMode.OpenOrCreate,
                                                                          FileAccess.Write, storage)) {
                        await fileStream.WriteAsync(file.bytes, 0, file.bytes.Length);
                    }

                    logger.debug("File saved successfully");
                    return filePath;
                }
            } else {
                throw new MTProtoFileUnavailableException();
            }
        }
开发者ID:Grief-Code,项目名称:kilogram,代码行数:34,代码来源:Files.cs

示例2: downloadFileAsync

			public async void downloadFileAsync(string url, IsolatedStorageFileStream filestream) {
				SynchronizationContext ctx = SynchronizationContext.Current;
				HttpWebRequest request = (HttpWebRequest)WebRequest.CreateHttp(new Uri(url));
				request.Method = "GET";

				Logger.log("DownloadRequestHandler", "Downloading " + url + " asynchronously...");

				request.BeginGetResponse(async callbackResult => {
					try {
						HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
						Logger.log("DownloadRequestHandler", "Status: " + (int)response.StatusCode + " " + response.StatusDescription);
						if (response.Headers["Content-Length"] != null) {
							Logger.log("DownloadRequestHandler", "Content length: " + response.Headers["Content-Length"]);
						} else {
							Logger.log("DownloadRequestHandler", "Content length: unknown");
						}

						Stream responseStream = response.GetResponseStream();
						BinaryReader br = new BinaryReader(responseStream);
						int responseBufferSize = 4096;
						int responseBytesRead = 0;
						int responseTotalBytesRead = 0;
						byte[] responseBuffer = new byte[responseBufferSize];

						while ((responseBytesRead = br.Read(responseBuffer, 0, responseBuffer.Length)) > 0) {
							responseTotalBytesRead += responseBytesRead;
							await filestream.WriteAsync(responseBuffer, 0, responseBuffer.Length);
						}
						filestream.Close();

						Logger.log("DownloadRequestHandler", "Wrote " + responseTotalBytesRead + " bytes");

						DownloadFileEventArgs args = new DownloadFileEventArgs();
						args.file = filestream.Name;
						args.size = responseTotalBytesRead;

						ctx.Post(result => {
							DownloadFile df = (DownloadFile)callbackResult.AsyncState;
							df.OnComplete((DownloadFileEventArgs)result);
						}, args);

					} catch (WebException ex) {
						// check if we have an expired or self-signed cert
						if (ex.Status == WebExceptionStatus.UnknownError) {
							if (ex.Response.Headers.Count == 0) {
								Logger.log("DownloadRequestHandler", "Invalid SSL certificate, returning a 400 Bad Request");
								throw new Exception("Download Handler Exception: Invalid SSL certificate, returning a 400 Bad Request");
							} else {
								Logger.log("DownloadRequestHandler", "File not found, returning a 404");
								throw new Exception("Download Handler Exception: File not found, returning a 404");
							}
						} else {
							Logger.log("DownloadRequestHandler", "400 Bad Request");
							Logger.log("DownloadRequestHandler", ex.Status.ToString());
							throw;
						}
					}
				}, this);
			}
开发者ID:nzoy,项目名称:titanium_mobile,代码行数:59,代码来源:DownloadRequestHandler.cs


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