Esempio n. 1
0
        private Stream OpenFileStream(string path, FileMode fileMode)
        {
            switch (fileMode)
            {
            case FileMode.Read:
                try {
                    return(File.OpenRead(path));
                } catch (Exception) {
                    return(null);
                }

            case FileMode.Write:
                try {
                    return(File.OpenWrite(path));
                } catch (Exception) {
                    return(null);
                }

            case FileMode.CreateNew:
                try {
                    return(File.Open(path, System.IO.FileMode.CreateNew, FileAccess.Write));
                } catch (Exception e) {
                    return(null);
                }

            default:
                throw new ArgumentOutOfRangeException(nameof(fileMode), fileMode, null);
            }
        }
Esempio n. 2
0
        public Stream OpenFile(string filename, StorageLocation storageLocation = StorageLocation.Documents, FileMode fileMode = FileMode.Read)
        {
            switch (storageLocation)
            {
            case StorageLocation.AppResource:
                if (fileMode == FileMode.Write || fileMode == FileMode.CreateNew)
                {
                    throw new NotSupportedException();
                }
                return(OpenFileStream(GetBundlePath(filename), fileMode));

            case StorageLocation.Documents:
            case StorageLocation.Cache:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(storageLocation), storageLocation, null);
            }
            return(OpenFileStream(GetFilePath(storageLocation, filename), fileMode));
        }
Esempio n. 3
0
        public Stream OpenFile(string filename, StorageLocation storageLocation = StorageLocation.Documents, FileMode fileMode = FileMode.Read)
        {
            string filesDirPath = null;

            switch (storageLocation)
            {
            case StorageLocation.AppResource:
                if (fileMode == FileMode.Write || fileMode == FileMode.CreateNew)
                {
                    throw new NotSupportedException();
                }
                return(OpenAssetFileStream(filename));

            case StorageLocation.Documents:
                filesDirPath = Application.Context.FilesDir.AbsolutePath;
                break;

            case StorageLocation.Cache:
                filesDirPath = Application.Context.CacheDir.AbsolutePath;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(storageLocation), storageLocation, null);
            }
            if (filesDirPath != null)
            {
                var path = Path.Combine(filesDirPath, filename);
                return(OpenFileStream(path, fileMode));
            }
            return(null);
        }