public async Task <string> SaveDataAsync(StorageFileWrapper <StorageFile> file, IWriter writer, Encoding encoding)
        {
            List <byte> allBytes = new List <byte>();

            if (file.BOM != null)
            {
                allBytes = new List <byte>(file.BOM);
                allBytes.AddRange(writer.GetBytes(encoding));
            }
            else
            {
                allBytes = new List <byte>(writer.GetBytes(encoding));
            }

            var bytes = allBytes.ToArray();


            if (IsFileReadOnly(file.File) || !await IsFileWritable(file.File))
            {
                // For file(s) dragged into Quick Pad, they are read-only
                // StorageFile API won't work but can be overwritten by Win32 PathIO API
                // In case the file is actually read-only, WriteBytesAsync will throw UnauthorizedAccessException exception
                await PathIO.WriteBytesAsync(file.Path, bytes);
            }
            else // Use FileIO API to save
            {
                // Create file; replace if exists.
                await FileIO.WriteBytesAsync(file.File, bytes);
            }

            return($"{file.Name} was saved.");
        }
示例#2
0
        public async Task <string> SaveDataAsync(StorageFileWrapper <StorageFile> file, IWriter writer, Encoding encoding)
        {
            try
            {
                List <byte> allBytes = new List <byte>();

                if (file.BOM != null)
                {
                    allBytes = new List <byte>(file.BOM);
                    allBytes.AddRange(writer.GetBytes(encoding));
                }
                else
                {
                    allBytes = new List <byte>(writer.GetBytes(encoding));
                }

                var bytes = allBytes.ToArray();

                // Create sample file; replace if exists.
                await FileIO.WriteBytesAsync(file.File, bytes);

                return($"{file.Name} was saved.");
            }
            catch (Exception)
            {
                throw;
            }
        }