示例#1
0
        public async Task <Stream> OpenFile(string fileName, FileModeEnum fileMode = FileModeEnum.Create)
        {
            fileName = FormatPathName(fileName);
            Stream stream = null;

            switch (fileMode)
            {
            case FileModeEnum.Create:
                stream = await localFolder.OpenStreamForWriteAsync(fileName, CreationCollisionOption.ReplaceExisting);

                break;

            case FileModeEnum.Open:
                stream = await localFolder.OpenStreamForReadAsync(fileName);

                break;

            case FileModeEnum.Append:
                stream = await localFolder.OpenStreamForWriteAsync(fileName, CreationCollisionOption.OpenIfExists);

                stream.Seek(0, SeekOrigin.End);
                break;
            }
            return(stream);
        }
示例#2
0
        public Task <Stream> OpenFile(string fileName, FileModeEnum fileMode = FileModeEnum.Create)
        {
            return(Task.Run <Stream>(delegate
            {
                string fullPath = Path.Combine(appExternalStorage, fileName);
                var parent = Directory.GetParent(fullPath);
                if (!parent.Exists)
                {
                    parent.Create();
                }

                switch (fileMode)
                {
                case FileModeEnum.Append:
                    return new FileStream(fullPath, FileMode.Append);

                case FileModeEnum.Create:
                    return new FileStream(fullPath, FileMode.Create);

                case FileModeEnum.Open:
                    return new FileStream(fullPath, FileMode.Open);

                default:
                    throw new InvalidProgramException("Unknown fileMode.");
                }
            }));
        }
示例#3
0
 private static async Task Write(string fileName, string message, FileModeEnum fileMode)
 {
     using (var stream = await GlobalAccess.DirectoryService.OpenFile(fileName, fileMode))
     {
         var bytes = Encoding.UTF8.GetBytes("\r\n" + DateTime.Now.ToLocalTime() + ",info:" + message);
         await stream.WriteAsync(bytes, 0, bytes.Length);
     }
 }
示例#4
0
    public static int Main()
    {
        FileModeEnum ac = new FileModeEnum();

        TestLibrary.TestFramework.BeginTestCase("FileModeEnum");

        if (ac.RunTests())
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("PASS");
            return 100;
        }
        else
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("FAIL");
            return 0;
        }
    }
示例#5
0
    public static int Main()
    {
        FileModeEnum ac = new FileModeEnum();

        TestLibrary.TestFramework.BeginTestCase("FileModeEnum");

        if (ac.RunTests())
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("PASS");
            return(100);
        }
        else
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("FAIL");
            return(0);
        }
    }
示例#6
0
        public async Task <Stream> OpenFile(string fileName, FileModeEnum fileMode = FileModeEnum.Create)
        {
            string fullFileName = Path.Combine(GetAppRootPath(), fileName);

            return(await Task.Run <Stream>(() =>
            {
                switch (fileMode)
                {
                case FileModeEnum.Create:
                    return File.Open(fullFileName, FileMode.Create);

                case FileModeEnum.Open:
                    return File.Open(fullFileName, FileMode.Open);

                default:
                    return File.Open(fullFileName, FileMode.Append);
                }
            }));
        }
示例#7
0
        public Task <Stream> OpenFile1(string fileName, FileModeEnum fileMode = FileModeEnum.Create)
        {
            return(Task.Run <Stream>(delegate
            {
                string fullPath = fileName.Replace("file://", "");

                switch (fileMode)
                {
                case FileModeEnum.Append:
                    return new FileStream(fullPath, FileMode.Append);

                case FileModeEnum.Create:
                    return new FileStream(fullPath, FileMode.Create);

                case FileModeEnum.Open:
                    return new FileStream(fullPath, FileMode.Open);

                default:
                    throw new InvalidProgramException("Unknown fileMode.");
                }
            }));
        }
示例#8
0
        public async Task <Stream> OpenFile(string fileName, FileModeEnum fileMode = FileModeEnum.Create)
        {
            FileMode curOsFileMode;

            switch (fileMode)
            {
            case FileModeEnum.Append:
                curOsFileMode = FileMode.Append;
                break;

            case FileModeEnum.Open:
                curOsFileMode = FileMode.Open;
                break;

            default:
                curOsFileMode = FileMode.OpenOrCreate;
                break;
            }
            string fullFilePath = GetAppRootPath() + fileName;
            string pureFileName = fileName;

            if (fileName.LastIndexOf("/") != -1)
            {
                pureFileName = fileName.Substring(fileName.LastIndexOf("/"));
            }
            if (File.Exists(fullFilePath))
            {
                return(File.Open(fullFilePath, curOsFileMode, FileAccess.Read, FileShare.Read));
            }
            else
            {
                await CreateDirectory(fullFilePath.Replace(pureFileName, ""));

                return(File.Create(fullFilePath));
            }
        }