DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Windows 8 Metro App Tip of The Day - reading/writing files without CAP declarations

Denzel D. user avatar by
Denzel D.
·
Dec. 19, 11 · Interview
Like (0)
Save
Tweet
Share
11.24K Views

Join the DZone community and get the full member experience.

Join For Free

Michael Crump, a fellow MVP, initiated an interesting discussion on Twitter today. He was wondering whether it was possible to write files to the Windows filesystem without additional capability declarations. After all, the only available storage-based options in the appxmanifest file are: Document Library Access, Picture Library Access and Removable Storage.

Let's say we have this piece of code:

async private void fileOpener_Click(object sender, RoutedEventArgs e)
{
    var Picker = new FileOpenPicker();
    Picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
    Picker.ViewMode = PickerViewMode.List;
    Picker.FileTypeFilter.Add(".jpg");

    var file = await Picker.PickSingleFileAsync();
    if (file != null)
    {
        var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

        BitmapImage image = new BitmapImage();
        image.SetSource(stream);
        image1.Source = image;
    }
}

Here I am using the regular file picker to read a JPEG image. Notice that my starting location is ComputerFolder, that is - the root representation of the entire machine. In theory, this means that I can access any physical drive. Or can I?

I can see that both C: (system drive) and D: (work drive) are accessible. More than that, I can access media servers that are available on my network. Data on D: is visible in its entirety. On the other hand, C: is being a bit more secretive - I cannot see Program Files and Windows.

This brings us to fact #1:

Protected system locations are not accesible through the public API in a Metro application.

Want to experiment more with it? Let's create a folder called TEST on the C: drive (or whatever your system drive letter might be).

To make the TEST folder hidden from the list of listed folders, simply add the System flag to it. This can be done by using the attrib +s command.

This brings us to fact #2:

A System flag will automaticlaly hide the folder from the default file picker.

You know now that you can read non-protected files without a CAP declaration, but can you write them?

Take a look at this snippet:

async private void fileWriter_Click(object sender, RoutedEventArgs e)
{
    IRandomAccessStream raStream;
    IOutputStream outputStream;
    DataWriter writer;
    StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(@"C:\Test");
    StorageFile file = await folder.CreateFileAsync("test.txt");

    raStream = await file.OpenAsync(FileAccessMode.ReadWrite);
    outputStream = raStream.GetOutputStreamAt(0);

    writer = new DataWriter(outputStream);

    writer.WriteString("TEST");

    await writer.StoreAsync();
}

The result will ultimately be an error - even though Test is a public folder, the application cannot write there because:

  • No capability allowing it to do so was declared
  • The file type TXT was not associated with the applicationĀ 

To fix this, you have to use KnownFolders to designate a specific location, be that the Documents or Pictures library. Both are associated with their own capabilities:

A file association is also necessary to avoid a runtime error:

That brings us to the final fact:

You cannot write files without properly declared capabilities and file associations.

app

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Best Use Java Records as DTOs in Spring Boot 3
  • Test Execution Tutorial: A Comprehensive Guide With Examples and Best Practices
  • AWS CodeCommit and GitKraken Basics: Essential Skills for Every Developer
  • What Are the Different Types of API Testing?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: