Asperger
Asperger

Reputation: 3222

Creating Files and Folders in UWP

Ive looked at at so many stackoverflow posts and articles but still didnt manage to create a file in UWP. In WPF it was really easy but UWP works differently.

I added the following in my manifest file:

  <Capabilities>
    <uap:Capability Name="documentsLibrary" />
  </Capabilities>

Now im not sure what to do next. Inside my documents folder I have a subfolder named "Project Files". I want to create folders and files in there. How is this done in UWP? I really dont understand.

Upvotes: 2

Views: 4618

Answers (2)

Xeorge Xeorge
Xeorge Xeorge

Reputation: 462

As microsoft states in their docs, its recommenced not to use the documents Library through an UWP app, instead opt for the built in storage unless its absolutely necessary.

There is an easy way to get around that if you use a folder picker

 private async void buttonClick(){
            FolderPicker folderPicker = new FolderPicker();
     folderPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
      folderPicker.FileTypeFilter.Add("*");

          StorageFolder folder=  await folderPicker.PickSingleFolderAsync();
         if (folder != null) { 

              //  do Things On Folder

          }
           else
          {
              MessageDialog dialog = new MessageDialog("you selected nothing");
             await dialog.ShowAsync();
          }

}

The above opens up a folder select dialog, it returns the folder the user picked, its the recommended way for accessing locations outside your app's folder.

Here is how to Create a new file in this folder:

string name ="myTitle.txt";
            await folder.CreateFileAsync(name, CreationCollisionOption.GenerateUniqueName);

here is how to open and write a file:

         try {
                           StorageFile myFile = await folder.GetFileAsync(name);
                           await Windows.Storage.FileIO.WriteTextAsync(myFile,  "myStringContent");

                               }
                       catch (Exception e)
                       {
                           Debug.WriteLine("Failure: "+e.Message);

                           return;
                       }

remember you can always avoid opening up a dialog if you use the local storage instead, it returns you app's storage folder in one line, like this:

 var folder= ApplicationData.Current.LocalFolder;

Upvotes: 5

kurakura88
kurakura88

Reputation: 2305

I believe in UWP using the documents library is neither recommended or permitted. See https://blogs.msdn.microsoft.com/wsdevsol/2013/05/09/dealing-with-documents-how-not-to-use-the-documentslibrary-capability-in-windows-store-apps/

If you side load the app and use the documents library capability the app gets access only to declared file types, not to everything in documents. See https://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx#special_capabilities

Note that this special capability will not let you pass through app certification in Store, unless you go through some special procedure contacting MS first.

To create folder, use StorageFolder. To create file, use StorageFile. See https://learn.microsoft.com/en-us/windows/uwp/files/quickstart-reading-and-writing-files

Upvotes: 2

Related Questions