Answered by:
StorageFile in UnitTest

Question
-
I working on a Windows Store App, and want to create a StorageFile in my UnitTest. The only examples I've seen for initializing a StorageFile object use OpenFilePicker. Having a UI pop-up in my unit test is not the goal.
Can someone point me toward a simple example of creating a StorageFile and setting the path/filename programmatically?
Thanks,
Randy
Thursday, February 20, 2014 8:12 PM
Answers
-
It took a bit of digging to understand this. The Documents Library is no longer there in Windows 8.1. Here's a thread that discusses that:
My purpose is to write a unit test that I would never deploy to the Windows Store. Using a TemporaryFolder is fine, even though the folder is deleted with the object goes out of scope.
All the pointers are appreciated. I've added my basic unit test code, and people can feel free to also click the vote button if they find it helpful.
Thanks!
[TestMethod] public async Task TestCreatingStorageFile() { // Select Folder location //var folder = ApplicationData.Current.LocalFolder; // OR //var folder = ApplicationData.Current.RoamingFolder; // OR StorageFolder folder = null; try { //folder = await KnownFolders.DocumentsLibrary.GetFolderAsync("TestFolder"); folder = ApplicationData.Current.TemporaryFolder; } catch (Exception e) { // Folder does not exist, but we cannot await in a catch block, // so we can not create it here. System.Diagnostics.Debug.WriteLine(e.Message); } if (folder == null) { try { folder = await ApplicationData.Current.TemporaryFolder.CreateFolderAsync("TestFolder", CreationCollisionOption.OpenIfExists); } catch(Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } } // Create File StorageFile file = await folder.CreateFileAsync("TestFile.txt", CreationCollisionOption.ReplaceExisting); // Verify that the file we just create exists. Debug.Assert(file.IsAvailable); }
Randy
- Marked as answer by Anne Jing Monday, March 3, 2014 3:18 AM
Friday, February 21, 2014 3:03 PM
All replies
-
Here are a couple of options to create a StorageFile without the need for a FilePicker:
// Select Folder location var folder = ApplicationData.Current.LocalFolder; // OR var folder = ApplicationData.Current.RoamingFolder; // OR StorageFolder folder = null; try { folder = await KnownFolders.DocumentsLibrary.GetFolderAsync("YourFolderNameHere"); } catch (Exception) { // Folder does not exist, but we cannot await in a catch block, so we can not create it here. } if (folder == null) { folder = await KnownFolders.DocumentsLibrary.CreateFolderAsync("YourFolderNameHere", CreationCollisionOption.OpenIfExists); } // Create File StorageFile file = await folder.CreateFileAsync("YourFileNameHere", CreationCollisionOption.ReplaceExisting);
- Marked as answer by Randy42Developer42 Friday, February 21, 2014 12:57 PM
- Unmarked as answer by Randy42Developer42 Friday, February 21, 2014 1:16 PM
Thursday, February 20, 2014 9:29 PM -
The response above seemed great, and made it look easy. Unfortunately, I keep getting an "Access is denied." exception.
I went into the Package.appxmanifest, Declaration tab, and added file Type Associations under the Available Declarations title. I'm still getting the "Access is denied." exception.
It looks like the code is right, so what else am I missing?
Thanks,
Randy
Friday, February 21, 2014 1:21 PM -
File Type Association as a Capability should be combined with 'Documents Library' in the Capabilities tab in the package manifest.
Don't forget to remove 'Documents Library' again before publishing to the Store, since it comes with extra requirements.
Friday, February 21, 2014 2:27 PM -
It took a bit of digging to understand this. The Documents Library is no longer there in Windows 8.1. Here's a thread that discusses that:
My purpose is to write a unit test that I would never deploy to the Windows Store. Using a TemporaryFolder is fine, even though the folder is deleted with the object goes out of scope.
All the pointers are appreciated. I've added my basic unit test code, and people can feel free to also click the vote button if they find it helpful.
Thanks!
[TestMethod] public async Task TestCreatingStorageFile() { // Select Folder location //var folder = ApplicationData.Current.LocalFolder; // OR //var folder = ApplicationData.Current.RoamingFolder; // OR StorageFolder folder = null; try { //folder = await KnownFolders.DocumentsLibrary.GetFolderAsync("TestFolder"); folder = ApplicationData.Current.TemporaryFolder; } catch (Exception e) { // Folder does not exist, but we cannot await in a catch block, // so we can not create it here. System.Diagnostics.Debug.WriteLine(e.Message); } if (folder == null) { try { folder = await ApplicationData.Current.TemporaryFolder.CreateFolderAsync("TestFolder", CreationCollisionOption.OpenIfExists); } catch(Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } } // Create File StorageFile file = await folder.CreateFileAsync("TestFile.txt", CreationCollisionOption.ReplaceExisting); // Verify that the file we just create exists. Debug.Assert(file.IsAvailable); }
Randy
- Marked as answer by Anne Jing Monday, March 3, 2014 3:18 AM
Friday, February 21, 2014 3:03 PM