troubleshooting Question

Accessing files and handling errors on Windows 8 with VS2012/C# Metro app

Avatar of elorc
elorcFlag for United States of America asked on
C#Windows 8
6 Comments2 Solutions1692 ViewsLast Modified:
I'm brand new to programming for Metro with Visual Studio 2012, and so far the process has been incredibly frustrating. I'm trying to write a simple app that opens a text file, changes it, and saves it. Easy, I'd expect, but apparently that's not the case! That, or I'm missing something. :)

1. My error handling sucks. I was trying to pop up a MessageDialog to notify the user of any exceptions that occur, but I learned that you can't have await (which means that you can't properly display the MessageDialog) within a catch block. I put together the solution shown below, but it's bulky and ugly. Surely there's a better way?

                        string ErrorMessage = "";
                        try
                        {
                            // do stuff here
                        }
                        catch (Exception ex)
                        {
                            ErrorMessage = ex.Message;
                        }
                        if (ErrorMessage != "")
                        {
                            MessageDialog mError = new MessageDialog(ErrorMessage, "Error");
                            await mError.ShowAsync();
                            ErrorMessage = "";
                        }




2. I can't seem to open files. At all. When I try something like this:

StorageFile file = await StorageFile.GetFileFromPathAsync(filePath);

It doesn't matter where the file is located -- c:\temp, My Documents, etc. -- the app throws an "access denied" exception. What am I missing?
ASKER CERTIFIED SOLUTION
Sammy

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros