I'm using this code to save and open a file using the Syncfusion DOCIO library.
using MemoryStream ms = new();
//Saves the Word document to the memory stream.
document.Save(ms, FormatType.Docx);
ms.Position = 0;
//Saves the memory stream as file.
SaveService saveService = new();
saveService.SaveAndView("FeeSheet.docx", "application/msword", ms);
But I want to change the code to Save the file to the following location:
// Get the app's local directory path
string filePath = Path.Combine(FileSystem.AppDataDirectory, "FeeSheet.docx");
And then Open the Word document like the code above does.
How do I make the changes to save to a new location and open the file using MS Word?
Thanks,
FTS
Hi Frederick,
From the provided details, we can be able to find that your requirement is to
save the Word document to the specified location and open the file using MS
Word application using our Syncfusion DocIO library with the .NET MAUI sample.
We have created a sample application for your requirement.
In the provided sample, for the Windows platform, we have saved the Word
document to a specified location and open it in the Microsoft Word application.
Kindly refer to the attached sample.
Regards,
Dharanya.
DHaranya,
I see that example in the Documentation.
However, can you show me how to just Save the Word Doc that I have created to the following .NET Maui location.
string filePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FeeSheet2.docx");
I've tried this:
//Saves the Word document to the memory stream.
using MemoryStream ms = new();
document.Save(ms, FormatType.Docx);
ms.Position = 0;
//Save the memory stream as a file.
SaveService saveService = new();
string filePath = Path.Combine(FileSystem.AppDataDirectory, "FeeSheet3.docx");
try
{
saveService.SaveAndView(filePath, "application/msword", ms);
}
catch (Exception ex)
{
DisplayAlert("Error", ex.Message, "OK");
}
But not seeing an Error and I'm not seeing my Word Document in the filePath Folder.
Frederick,
Regarding “can you show me how to just Save the Word Doc that I have created
to the following .NET Maui location.”:
We have modified the code in the Windows platform SaveWindows.cs file to save
the Word document to the specified location and open the file using the MS Word
application. Please refer to the modified code below:
|
public async partial void SaveAndView(string filename, string contentType, MemoryStream stream) { try { // Save the document to the specified full path. using (FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write)) { // Write the data from the memory stream to the file stream. stream.WriteTo(fileStream); }
// Optionally, prompt the user if they want to view the file. MessageDialog msgDialog = new("Do you want to view the document?", "File has been saved successfully"); UICommand yesCmd = new("Yes"); msgDialog.Commands.Add(yesCmd); UICommand noCmd = new("No"); msgDialog.Commands.Add(noCmd);
IntPtr windowHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; WinRT.Interop.InitializeWithWindow.Initialize(msgDialog, windowHandle);
// Show dialog box. IUICommand cmd = await msgDialog.ShowAsync(); if (cmd.Label == yesCmd.Label) { // Launch the saved file. StorageFile savedFile = await StorageFile.GetFileFromPathAsync(filename); await Windows.System.Launcher.LaunchFileAsync(savedFile); } } catch (Exception ex) { // Handle any exceptions that might occur. MessageDialog errorDialog = new($"An error occurred: {ex.Message}", "Error"); UICommand okCmd = new("Ok"); errorDialog.Commands.Add(okCmd); WinRT.Interop.InitializeWithWindow.Initialize(errorDialog, System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle); await errorDialog.ShowAsync(); } } |
By modifying the SaveAndView() method in the SaveWindows.cs file in your
application and specifying the full path along with the sample name in MainPage.xaml.cs,
you can save the Word document to the desired location and open the file using
the MS Word application.
If you are still facing issues, kindly confirm whether the problem occurs after
deploying the attached sample.
Regards,
Dharanya.
Thanks Dhartanya that worked.
How does the SaveWindow.cs file get added to the Windows Platform Folder?
Hi Frederick,
Regarding “How does the SaveWindow.cs file get added to the Windows Platform
Folder?”:
We use a partial class called SaveService, and its complete saving logic
varies based on the platform, such as Windows, Android, or iOS. For each
platform, we provide a platform-specific implementation. In the case of the
Windows platform, the SaveWindow.cs file contains the partial implementation of
SaveService. This file is included in the Windows platform folder as per the
project structure.
Regards,
Dharanya.