Reputation: 312
So I have put an excel document in my assets folder for testing. And I am working on a test project just to see how the code works.
If I run the following code:
public async void TestAsync()
{
StorageFolder storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile sampleFile = await storageFolder.GetFileAsync(@"Assets\Generator_Run_Data.xlsx");
byte[] result;
using (Stream stream = await sampleFile.OpenStreamForReadAsync())
{
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
result = memoryStream.ToArray();
}
}
}
to which I get a result {byte[48347]}
But on the following code I get an error:
public async static void CreateSpreadsheetWorkbook()
{
StorageFolder storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
storageFolder = await storageFolder.GetFolderAsync(@"Assets");
// Create a spreadsheet document by supplying the filepath.
// By default, AutoSave = true, Editable = true, and Type = xlsx.
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(storageFolder.Path, SpreadsheetDocumentType.Workbook);
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
sheets.Append(sheet);
workbookpart.Workbook.Save();
// Close the document.
spreadsheetDocument.Close();
}
The error being:
System.UnauthorizedAccessException
HResult=0x80070005
Message=Access to the path 'C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\bin\x86\Debug\AppX\Assets' is denied.
Source=System.IO.FileSystem
StackTrace:
at System.IO.Win32FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.MultiplexingWin32WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.Packaging.ZipPackage..ctor(String path, FileMode packageFileMode, FileAccess packageFileAccess, FileShare share)
at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare)
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.CreateCore(String path)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Create(String path, SpreadsheetDocumentType type, Boolean autoSave)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Create(String path, SpreadsheetDocumentType type)
at GeneratedCode.MainPage.<CreateSpreadsheetWorkbook>d__2.MoveNext() in C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\MainPage.xaml.cs:line 68
So the folder is accessible in one case and not in another. Help me to understand please.
Tried this code as well:
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(sampleFileString, true))
and also received an error:
System.UnauthorizedAccessException
HResult=0x80070005
Message=Access to the path 'C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\bin\x86\Debug\AppX\Assets\Generator_Run_Data.xlsx' is denied.
Source=System.IO.FileSystem
StackTrace:
at System.IO.Win32FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.MultiplexingWin32WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.Packaging.ZipPackage..ctor(String path, FileMode packageFileMode, FileAccess packageFileAccess, FileShare share)
at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare)
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.OpenCore(String path, Boolean readWriteMode)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable, OpenSettings openSettings)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable)
at GeneratedCode.MainPage.Spreadsheet(String[] args) in C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\MainPage.xaml.cs:line 105
at GeneratedCode.MainPage..ctor() in C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\MainPage.xaml.cs:line 42
at GeneratedCode.GeneratedCode_XamlTypeInfo.XamlTypeInfoProvider.Activate_0_MainPage() in C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\obj\x86\Debug\XamlTypeInfo.g.cs:line 178
at GeneratedCode.GeneratedCode_XamlTypeInfo.XamlUserType.ActivateInstance() in C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\obj\x86\Debug\XamlTypeInfo.g.cs:line 333
Upvotes: 1
Views: 1263
Reputation: 32775
Message=Access to the path 'C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\bin\x86\Debug\AppX\Assets' is denied
.
The problem is the app's install directory is a read-only location. You can't gain access to the install directory through the file picker.
In general, we often store the file in localFolder
that could be access directly.
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
For more please refer Application data locations official documentation.
Upvotes: 2