Home > Blockchain >  .Net Maui copying assets into appData
.Net Maui copying assets into appData

Time:12-04

Tring to move a PDF file from Resources\Raw to appData directory for App. File becomes corrupted on copy. Obviously I'm missing something.

using the following:

  using var stream = await FileSystem.OpenAppPackageFileAsync("CBA2015.pdf");
        using var reader = new StreamReader(stream);

        if (stream != null)
        {
            var contents = reader.ReadToEnd();
            string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "CBA2015.pdf");

            using FileStream outputStream = System.IO.File.OpenWrite(targetFile);
            using StreamWriter streamWriter = new StreamWriter(outputStream);

            await streamWriter.WriteAsync(contents);

        }

CodePudding user response:

To move a file from one directory to another, you can use the File.Move method. This method takes two arguments: the path to the source file and the path to the destination file.

Here is an example of how you can use File.Move to move a file:

string sourceFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "CBA2015.pdf");
string destinationFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "CBA2015_moved.pdf");

System.IO.File.Move(sourceFile, destinationFile);

In this example, the sourceFile is the path to the file you want to move, and the destinationFile is the path to the directory where you want to move the file to. If the file is successfully moved, you should be able to access it at the destinationFile path.

You can also use the File.Copy method to copy the file from one directory to another, instead of moving it. This method takes the same two arguments as File.Move: the path to the source file and the path to the destination file. Here is an example of how you can use File.Copy to copy a file:

string sourceFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "CBA2015.pdf");
string destinationFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "CBA2015_copy.pdf");

System.IO.File.Copy(sourceFile, destinationFile);

In this example, the sourceFile is the path to the file you want to copy, and the destinationFile is the path to the directory where you want to copy the file to. If the file is successfully copied, you should be able to access it at the destinationFile path.

I hope this helps!

CodePudding user response:

This works:

string sourceFile = "CBA2015.pdf"; // Read the source file //using Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(sourceFile); // using StreamReader reader = new StreamReader(fileStream);

        string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "CBA2015.pdf");
        using FileStream outputStream = System.IO.File.OpenWrite(targetFile);
        using Stream fs = await FileSystem.Current.OpenAppPackageFileAsync(sourceFile);
        using BinaryWriter writer = new BinaryWriter(outputStream);
        using (BinaryReader reader = new BinaryReader(fs))
        {
            var bytesRead = 0;
            
            int bufferSize = 1024;
            byte[] bytes;
            var buffer = new byte[bufferSize];
            using (fs)
            {
                do
                {

                    buffer = reader.ReadBytes(bufferSize);
                    bytesRead = buffer.Count();
                    writer.Write(buffer);

                }

                while (bytesRead > 0);

            }
        }
  • Related