Exemple #1
0
        public void Test_FileAsync_Read()
        {
            var path   = AppDomain.CurrentDomain.BaseDirectory + "\\a.txt";
            var result = FileAsync.ReadAllText(path).Result;

            Assert.IsNotNull(result);
        }
        ///<summary>
        /// 비동기 방식으로 파일의 전체 내용을 읽어 문자열로 반환합니다.
        /// </summary>
        /// <param name="filepath">읽을 파일의 전체 경로</param>
        /// <param name="encoding">인코딩 방식</param>
        /// <returns>파일 내용</returns>
        public static Task <string> ReadTextTask(string filepath, Encoding encoding = null)
        {
            if (IsDebugEnabled)
            {
                log.Debug("파일 내용을 비동기 방식으로 문자열로 모두 읽어옵니다... filepath=[{0}], encoding=[{1}]", filepath, encoding);
            }

            return(FileAsync.ReadAllText(filepath, encoding));
        }
        /// <inheritdoc />
        public async Task <FileContent> ReadFile(string subPath)
        {
            Check.NotNull(subPath, nameof(subPath));

            var    fullPath = GetFullPath(subPath);
            string text     = string.Empty;

            if (File.Exists(fullPath))
            {
                text = await FileAsync.ReadAllText(fullPath);
            }
            return(new FileContent(fullPath, text));
        }
        public void Can_WriteAsync_And_ReadAllText()
        {
            if (IsDebugEnabled)
            {
                log.Debug("Write Asynchronous and Real all text...");
            }

            var filename = FileTool.GetTempFileName("ASYNC_");

            using (var fs = FileAsync.OpenWrite(filename)) {
                Task chainTask = null;

                for (int i = 0; i < RunCount; i++)
                {
                    var lineNo = i.ToString() + ": ";
                    var buffer = Encoding.UTF8.GetBytes(lineNo);

                    if (chainTask == null)
                    {
                        chainTask = fs.WriteAsync(buffer, 0, buffer.Length);
                    }
                    else
                    {
                        chainTask.ContinueWith(_ => fs.WriteAsync(buffer, 0, buffer.Length));
                    }

                    chainTask = chainTask.ContinueWith(_ => fs.WriteAsync(MessageBuffer, 0, MessageBuffer.Length));
                }

                chainTask.Wait();
            }

            var fileText = With.TryFunctionAsync(() => FileAsync.ReadAllText(filename, Encoding.UTF8).Result, () => string.Empty);

            Assert.IsNotEmpty(fileText);

            Console.WriteLine("Contents = " + Environment.NewLine + fileText);

            With.TryAction(() => FileTool.DeleteFile(filename));
        }